1# Copyright (c) 2021 Huawei Device Co., Ltd. 2# Licensed under the Apache License, Version 2.0 (the "License"); 3# you may not use this file except in compliance with the License. 4# You may obtain a copy of the License at 5# 6# http://www.apache.org/licenses/LICENSE-2.0 7# 8# Unless required by applicable law or agreed to in writing, software 9# distributed under the License is distributed on an "AS IS" BASIS, 10# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11# See the License for the specific language governing permissions and 12# limitations under the License. 13 14import("//build/ohos.gni") 15import("//build/test.gni") 16 17declare_args() { 18 SUITES_OUTPUT_ROOT = "$root_out_dir/suites" 19 SUITE_ARCHIVE_DIR = "$root_out_dir/suites/archives" 20 TESTCONFIG_FILENAME = "Test.xml" 21 XTS_ROOT = "//test/xts" 22 ACTS_ROOT = "//test/xts/acts" 23 HATS_ROOT = "//test/xts/hats" 24 HITS_ROOT = "//test/xts/hits" 25 DCTS_ROOT = "//test/xts/dcts" 26 27 # create testsuite archive is time-consuming, do it only if necessary 28 make_archive = false 29 XTS_SUITENAME = getenv("XTS_SUITENAME") 30} 31 32template("ohos_testsuite_base") { 33 assert(defined(invoker.project_type), 34 "project_type is required in target ${target_name}") 35 36 _project_type = invoker.project_type 37 _subsystem_name = "" 38 if (defined(invoker.subsystem_name)) { 39 _subsystem_name = invoker.subsystem_name 40 } 41 42 _test_files = "" 43 if (defined(invoker.generated_testfiles)) { 44 foreach(file, invoker.generated_testfiles) { 45 _test_files = 46 _test_files + "," + rebase_path("$root_gen_dir") + "/" + file 47 } 48 } 49 50 _is_testbundle = defined(invoker.is_testbundle) && invoker.is_testbundle 51 52 if (defined(invoker.sub_output_dir)) { 53 _output_file_dir = 54 rebase_path("$root_out_dir/tests/moduletest/${invoker.sub_output_dir}") 55 } else if (defined(invoker.module_out_path)) { 56 _output_file_dir = 57 rebase_path("$root_out_dir/tests/moduletest/${invoker.module_out_path}") 58 } else { 59 _output_file_dir = rebase_path("$root_out_dir/tests/moduletest") 60 } 61 _output_file = "${_output_file_dir}/module_${target_name}" 62 63 if (_project_type == "gtest" || _project_type == "ctestbundle") { 64 _output_file = "${_output_file_dir}/${target_name}" 65 _archive_filename = "${target_name}" 66 67 target("ohos_moduletest", "module_${target_name}") { 68 forward_variables_from(invoker, "*") 69 testonly = true 70 } 71 } else if (_project_type == "zunit" || _project_type == "javatestbundle") { 72 _output_file = "${_output_file_dir}/module_${target_name}.dex" 73 _archive_filename = "${target_name}.dex" 74 75 target("ohos_java_moduletest", "module_${target_name}") { 76 forward_variables_from(invoker, "*") 77 testonly = true 78 } 79 } else if (_project_type == "hostjunit") { 80 _output_file = "${_output_file_dir}/module_${target_name}.jar" 81 if (defined(invoker.final_jar_path)) { 82 _output_file = invoker.final_jar_path 83 } 84 _archive_filename = "${target_name}.jar" 85 86 target("java_library", "module_${target_name}") { 87 forward_variables_from(invoker, "*") 88 is_host_library = true 89 } 90 } else if (_project_type == "testhap" || _project_type == "haptestbundle") { 91 assert(defined(invoker.hap_name), 92 "hap_name is required in target ${target_name}") 93 assert(!defined(invoker.final_hap_path), 94 "please use hap_name instead of final_hap_path") 95 96 _hap_name = invoker.hap_name 97 _final_hap_path = "${SUITES_OUTPUT_ROOT}/haps/${_hap_name}.hap" 98 _output_file = _final_hap_path 99 _archive_filename = "${_hap_name}.hap" 100 101 target("ohos_hap", "module_${target_name}") { 102 forward_variables_from(invoker, "*") 103 subsystem_name = XTS_SUITENAME 104 final_hap_path = _final_hap_path 105 testonly = true 106 } 107 } else if (_project_type == "pythontest") { 108 if (defined(invoker.outputs_dir)) { 109 _out_put_dir = invoker.outputs_dir 110 _archive_filename = "${_subsystem_name}/${_out_put_dir}" 111 } else { 112 _archive_filename = "${_subsystem_name}" 113 } 114 _test_files = invoker.output_file 115 _deps = [] 116 if (defined(invoker.deps)) { 117 _deps = invoker.deps 118 } 119 } else if (_project_type == "js_test_hap") { 120 _hap_name = invoker.test_hap_name 121 _archive_filename = "${_hap_name}.hap" 122 _output_file = invoker.hap_source_path 123 } 124 125 _apilibrary_deps = "" 126 if (_is_testbundle && defined(invoker.deps)) { 127 foreach(dep, invoker.deps) { 128 _apilibrary_deps = _apilibrary_deps + "," + dep 129 } 130 } 131 132 if (_project_type != "pythontest" && _project_type != "js_test_hap") { 133 _deps = [ ":module_${target_name}" ] 134 } else { 135 _deps = [] 136 } 137 _suite_out_dir = "${SUITES_OUTPUT_ROOT}/${XTS_SUITENAME}" 138 _archive_testfile = "${_suite_out_dir}/testcases/${_archive_filename}" 139 _arguments = [ 140 "build_module_with_testbundle", 141 "--build_gen_dir", 142 rebase_path("$root_gen_dir"), 143 "--build_target_name", 144 target_name, 145 "--buildgen_testfile", 146 rebase_path(_output_file), 147 "--project_path", 148 rebase_path("."), 149 "--test_xml", 150 rebase_path(TESTCONFIG_FILENAME), 151 "--project_type", 152 _project_type, 153 "--suite_out_dir", 154 rebase_path("${_suite_out_dir}"), 155 "--archive_testfile", 156 rebase_path("${_archive_testfile}"), 157 ] 158 159 if (_subsystem_name != "") { 160 _arguments += [ 161 "--subsystem_name", 162 _subsystem_name, 163 ] 164 } 165 166 if (_apilibrary_deps != "") { 167 _arguments += [ 168 "--apilibrary_deps", 169 _apilibrary_deps, 170 ] 171 } 172 173 if (_test_files != "") { 174 _arguments += [ 175 "--test_files", 176 _test_files, 177 ] 178 } 179 180 action(target_name) { 181 deps = _deps 182 script = rebase_path("//test/xts/tools/build/suite.py") 183 args = _arguments 184 outputs = [ _archive_testfile ] 185 testonly = true 186 } 187} 188 189template("ohos_moduletest_suite") { 190 target("ohos_testsuite_base", "${target_name}") { 191 forward_variables_from(invoker, "*") 192 if (!defined(module_out_path)) { 193 module_out_path = "xts/modules" 194 } 195 project_type = "gtest" 196 } 197} 198 199template("ohos_hap_suite") { 200 target("ohos_testsuite_base", "${target_name}") { 201 forward_variables_from(invoker, "*") 202 203 # auto add HJUnitRunner entry ability and test-framework 204 if (defined(hap_profile)) { 205 # NOTE:: the GN tool disallow source files located in the ${out_dir} 206 # so we put the generated files in the xts dir. remember to REMOVE these. 207 _profile_relative_path = rebase_path(hap_profile, rebase_path(XTS_ROOT)) 208 _fixed_profile_path = 209 "${XTS_ROOT}/autogen_apiobjs/${_profile_relative_path}" 210 _fixer_script = rebase_path( 211 "//test/xts/tools/build/adapter/haptest_manifest_fixer.py") 212 exec_script(_fixer_script, 213 [ 214 "add_entryability", 215 "--raw_file=" + rebase_path(hap_profile), 216 "--dest_file=" + rebase_path(_fixed_profile_path), 217 ]) 218 hap_profile = _fixed_profile_path 219 } 220 221 if (defined(deps)) { 222 deps += [ "//test/xts/tools/hjunit:testkit_harmonyjunitrunner_java" ] 223 } else { 224 deps = [ "//test/xts/tools/hjunit:testkit_harmonyjunitrunner_java" ] 225 } 226 227 project_type = "testhap" 228 } 229} 230 231template("ohos_js_hap_suite") { 232 target("ohos_testsuite_base", "${target_name}") { 233 forward_variables_from(invoker, "*") 234 project_type = "testhap" 235 js_build_mode = "debug" 236 } 237} 238 239template("ohos_shell_app_suite") { 240 target("ohos_adapter_shell_app_suite", "${target_name}") { 241 forward_variables_from(invoker, "*") 242 } 243} 244 245template("ohos_prebuilt_suite") { 246 assert(!defined(invoker.source_files) || !defined(invoker.jar_path) || 247 !defined(invoker.source_dir), 248 "source_files, jar_path or source_dir must be specified one.") 249 assert(!defined(invoker.final_jar_path), 250 "final_jar_path is not allowed in target ${target_name}") 251 252 if (defined(invoker.jar_path)) { 253 _output_name = "${target_name}.jar" 254 if (defined(invoker.output_name)) { 255 _output_name = "${invoker.output_name}.jar" 256 } 257 258 _output_type = "tools" 259 if (defined(invoker.output_type)) { 260 _output_type = invoker.output_type 261 } 262 263 _final_jar_path = 264 "${SUITES_OUTPUT_ROOT}/${XTS_SUITENAME}/${_output_type}/${_output_name}" 265 266 target("java_prebuilt", "${target_name}") { 267 forward_variables_from(invoker, "*") 268 final_jar_path = _final_jar_path 269 is_host_library = true 270 } 271 } else { 272 assert(defined(invoker.output_dir), 273 "output_dir is require in target ${target_name}") 274 _outputs = [] 275 _copy_args = [ 276 "--method_name", 277 "copy_file", 278 ] 279 _deps = [] 280 _output_dir = "${SUITES_OUTPUT_ROOT}/${XTS_SUITENAME}/${invoker.output_dir}" 281 if (defined(invoker.source_dir)) { 282 _copy_args += [ 283 "--arguments", 284 "output=" + rebase_path(_output_dir) + "#source_dirs=" + 285 rebase_path(invoker.source_dir) + "#to_dir=True", 286 ] 287 _outputs = [ _output_dir ] 288 } else if (defined(invoker.source_files)) { 289 _sources = "" 290 foreach(src, invoker.source_files) { 291 _sources = _sources + rebase_path(src) + "," 292 } 293 _copy_args += [ 294 "--arguments", 295 "output=" + rebase_path(_output_dir) + "#sources=" + _sources + 296 "#to_dir=True", 297 ] 298 _outputs = [ _output_dir ] 299 } 300 if (defined(invoker.deps)) { 301 _deps = invoker.deps 302 } 303 action(target_name) { 304 script = rebase_path("//test/xts/tools/build/utils.py") 305 deps = _deps 306 args = _copy_args 307 outputs = _outputs 308 } 309 } 310} 311 312template("ohos_deploy_xdevice") { 313 _suite_out_dir = "${SUITES_OUTPUT_ROOT}/${XTS_SUITENAME}" 314 315 _args = [ 316 "build_xdevice", 317 "--source_dir", 318 rebase_path("//test/xdevice"), 319 "--suite_out_dir", 320 rebase_path(_suite_out_dir), 321 ] 322 323 if (defined(invoker.configs_dir)) { 324 _args += [ 325 "--configs_dir", 326 rebase_path(rebase_path(invoker.configs_dir)), 327 ] 328 } 329 330 if (defined(invoker.resources_dir)) { 331 _args += [ 332 "--resources_dir", 333 rebase_path(rebase_path(invoker.resources_dir)), 334 ] 335 } 336 337 action(target_name) { 338 testonly = true 339 script = rebase_path("//test/xts/tools/build/suite.py") 340 args = _args 341 outputs = [ 342 "${_suite_out_dir}/tools/xdevice-extension-0.0.0.tar.gz", 343 "${_suite_out_dir}/tools/xdevice-0.0.0.tar.gz", 344 "${_suite_out_dir}/tools/run.sh", 345 "${_suite_out_dir}/tools/run.bat", 346 ] 347 } 348} 349 350template("ohos_test_suite") { 351 _output = "${SUITES_OUTPUT_ROOT}/${target_name}.zip" 352 _suite_path = rebase_path("${SUITES_OUTPUT_ROOT}/${XTS_SUITENAME}") 353 _suite_archive_dir = rebase_path("${SUITE_ARCHIVE_DIR}") 354 _prebuilts_files = rebase_path("//xts/resource") 355 _gen_args = [ 356 "archive_suite", 357 "--suite_path", 358 _suite_path, 359 "--prebuilts_resource", 360 _prebuilts_files, 361 "--suite_archive_dir", 362 _suite_archive_dir, 363 "--make_archive", 364 "${make_archive}", 365 ] 366 367 _deps = [] 368 if (defined(invoker.deps)) { 369 _deps += invoker.deps 370 } 371 372 action(target_name) { 373 testonly = true 374 script = rebase_path("//test/xts/tools/build/suite.py") 375 deps = _deps 376 args = _gen_args 377 outputs = [ _output ] 378 } 379} 380 381template("pythontest_suite") { 382 assert(defined(invoker.script), "script is required in target ${target_name}") 383 384 _subsystem_name = "" 385 if (defined(invoker.subsystem_name)) { 386 _subsystem_name = invoker.subsystem_name 387 } else { 388 _local_path = rebase_path(".") 389 _args1 = [ 390 "--method_name", 391 "get_subsystem_name", 392 "--arguments", 393 "path=${_local_path}", 394 ] 395 _subsystem_name = 396 exec_script(rebase_path("//test/xts/tools/build/utils.py"), 397 _args1, 398 "trim string") 399 } 400 _deps = [] 401 if (defined(invoker.deps)) { 402 _deps = invoker.deps + _deps 403 } 404 _outputs_dir = "" 405 if (defined(invoker.outputs_dir)) { 406 _outputs_dir = invoker.outputs_dir 407 } 408 _output_file = rebase_path("${invoker.script}") 409 410 target("ohos_testsuite_base", "${target_name}") { 411 project_type = "pythontest" 412 subsystem_name = _subsystem_name 413 output_file = _output_file 414 deps = _deps 415 outputs_dir = _outputs_dir 416 } 417} 418 419template("executable_suite") { 420 assert(defined(invoker.suite_name), 421 "suite_name is required in target ${target_name}") 422 _suite_name = invoker.suite_name 423 _local_path = rebase_path(".") 424 _args1 = [ 425 "--method_name", 426 "get_subsystem_name", 427 "--arguments", 428 "path=${_local_path}", 429 ] 430 _subsystem_name = exec_script(rebase_path("//test/xts/tools/build/utils.py"), 431 _args1, 432 "trim string") 433 434 _outputs_dir = "" 435 if (defined(invoker.outputs_dir)) { 436 _outputs_dir = "${invoker.outputs_dir}" 437 } 438 ohos_executable(target_name) { 439 forward_variables_from(invoker, 440 "*", 441 [ 442 "test_type", 443 "module_out_path", 444 "visibility", 445 ]) 446 forward_variables_from(invoker, [ "visibility" ]) 447 if (!defined(deps)) { 448 deps = [] 449 } 450 451 subsystem_name = "tests" 452 part_name = "ssts" 453 ohos_test = true 454 testonly = true 455 output_name = "$target_name" 456 test_output_dir = "$SUITES_OUTPUT_ROOT/${_suite_name}/testcases/${_subsystem_name}/${_outputs_dir}" 457 if (defined(invoker.output_extension)) { 458 output_extension = invoker.output_extension 459 } 460 } 461} 462 463template("js_hap_suite") { 464 assert(defined(invoker.hap_source_path), 465 "hap_source_path is required in target ${target_name}") 466 assert(defined(invoker.test_hap_name), 467 "test_hap_name is required in target ${target_name}") 468 if (defined(invoker.deps)) { 469 _deps = invoker.deps 470 } 471 target("ohos_testsuite_base", "${target_name}") { 472 forward_variables_from(invoker, "*") 473 project_type = "js_test_hap" 474 } 475} 476