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 _part_name = "common" 37 _subsystem_name = "common" 38 if (defined(invoker.part_name)) { 39 _part_name = invoker.part_name 40 } 41 if (defined(invoker.subsystem_name)) { 42 _subsystem_name = invoker.subsystem_name 43 } 44 _project_type = invoker.project_type 45 46 _build_part_boolean = false 47 tmp_subsystem_part = "${_subsystem_name}_${_part_name}" 48 _part_script_judge = "//test/xts/tools/build/judgePart.py" 49 _script_judge_args1 = [ 50 rebase_path("$preloader_output_dir") + "/parts_config.json", 51 tmp_subsystem_part, 52 "judgePart", 53 ] 54 _build_part_boolean_str = exec_script(rebase_path(_part_script_judge), 55 _script_judge_args1, 56 "trim string") 57 if (_build_part_boolean_str == "True") { 58 _build_part_boolean = true 59 } 60 61 if (_subsystem_name == "kernel" || _subsystem_name == "common") { 62 _build_part_boolean = true 63 } 64 65 _test_files = "" 66 if (defined(invoker.generated_testfiles)) { 67 foreach(file, invoker.generated_testfiles) { 68 _test_files = 69 _test_files + "," + rebase_path("$root_gen_dir") + "/" + file 70 } 71 } 72 73 _is_testbundle = defined(invoker.is_testbundle) && invoker.is_testbundle 74 75 if (defined(invoker.sub_output_dir)) { 76 _output_file_dir = 77 rebase_path("$root_out_dir/tests/moduletest/${invoker.sub_output_dir}") 78 } else if (defined(invoker.module_out_path)) { 79 _output_file_dir = 80 rebase_path("$root_out_dir/tests/moduletest/${invoker.module_out_path}") 81 } else { 82 _output_file_dir = rebase_path("$root_out_dir/tests/moduletest") 83 } 84 _output_file = "${_output_file_dir}/module_${target_name}" 85 86 if (_project_type == "gtest" || _project_type == "ctestbundle") { 87 _output_file = "${_output_file_dir}/${target_name}" 88 _archive_filename = "${target_name}" 89 if (_build_part_boolean == true) { 90 target("ohos_moduletest", "module_${target_name}") { 91 forward_variables_from(invoker, "*") 92 testonly = true 93 } 94 } else { 95 print(tmp_subsystem_part + " is not build") 96 if (defined(invoker.public_configs)) { 97 print(invoker.public_configs) 98 } 99 if (defined(invoker.external_deps)) { 100 print(invoker.external_deps) 101 } 102 if (defined(invoker.deps)) { 103 print(invoker.deps) 104 } 105 if (defined(invoker.configs)) { 106 print(invoker.configs) 107 } 108 if (defined(invoker.sources)) { 109 print(invoker.sources) 110 } 111 } 112 } else if (_project_type == "zunit" || _project_type == "javatestbundle") { 113 _output_file = "${_output_file_dir}/module_${target_name}.dex" 114 _archive_filename = "${target_name}.dex" 115 if (_build_part_boolean == true) { 116 target("ohos_java_moduletest", "module_${target_name}") { 117 forward_variables_from(invoker, "*") 118 testonly = true 119 } 120 } 121 } else if (_project_type == "hostjunit") { 122 _output_file = "${_output_file_dir}/module_${target_name}.jar" 123 if (defined(invoker.final_jar_path)) { 124 _output_file = invoker.final_jar_path 125 } 126 _archive_filename = "${target_name}.jar" 127 if (_build_part_boolean == true) { 128 target("java_library", "module_${target_name}") { 129 forward_variables_from(invoker, "*") 130 is_host_library = true 131 } 132 } 133 } else if (_project_type == "testhap" || _project_type == "haptestbundle" || 134 _project_type == "testhapassist" || _project_type == "testapp" || 135 _project_type == "testappassist") { 136 assert(defined(invoker.hap_name), 137 "hap_name is required in target ${target_name}") 138 assert(!defined(invoker.final_hap_path), 139 "please use hap_name instead of final_hap_path") 140 141 _hap_name = invoker.hap_name 142 _final_hap_path = "" 143 _target_name = "" 144 if (_project_type == "testhapassist") { 145 _final_hap_path = 146 "${SUITES_OUTPUT_ROOT}/${XTS_SUITENAME}/testcases/${_hap_name}.hap" 147 _target_name = target_name 148 } else { 149 _final_hap_path = "${SUITES_OUTPUT_ROOT}/haps/${_hap_name}.hap" 150 _target_name = "module_${target_name}" 151 } 152 _output_file = _final_hap_path 153 _archive_filename = "${_hap_name}.hap" 154 if (_build_part_boolean == true) { 155 if (_project_type == "testapp") { 156 target("ohos_app", _target_name) { 157 forward_variables_from(invoker, "*") 158 subsystem_name = XTS_SUITENAME 159 hap_out_dir = "${SUITES_OUTPUT_ROOT}/haps" 160 testonly = true 161 } 162 } else if (_project_type == "testappassist") { 163 target("ohos_app", target_name) { 164 forward_variables_from(invoker, "*") 165 subsystem_name = XTS_SUITENAME 166 hap_out_dir = "${SUITES_OUTPUT_ROOT}/${XTS_SUITENAME}/testcases" 167 testonly = true 168 } 169 } else { 170 target("ohos_hap", _target_name) { 171 forward_variables_from(invoker, "*") 172 subsystem_name = XTS_SUITENAME 173 final_hap_path = _final_hap_path 174 testonly = true 175 } 176 } 177 } else { 178 print(tmp_subsystem_part + " is not build") 179 not_needed(invoker, "*") 180 } 181 } else if (_project_type == "pythontest") { 182 if (defined(invoker.outputs_dir)) { 183 _out_put_dir = invoker.outputs_dir 184 _archive_filename = "${_subsystem_name}/${_out_put_dir}" 185 } else { 186 _archive_filename = "${_subsystem_name}" 187 } 188 _test_files = invoker.output_file 189 _deps = [] 190 if (defined(invoker.deps)) { 191 _deps = invoker.deps 192 } 193 } else if (_project_type == "js_test_hap") { 194 _hap_name = invoker.test_hap_name 195 _output_file = invoker.hap_source_path 196 _archive_filename = "${_hap_name}.hap" 197 } 198 199 _apilibrary_deps = "" 200 if (_is_testbundle && defined(invoker.deps)) { 201 foreach(dep, invoker.deps) { 202 _apilibrary_deps = _apilibrary_deps + "," + dep 203 } 204 } 205 206 if (_project_type != "pythontest" && _project_type != "js_test_hap" && 207 _project_type != "testhapassist" && _project_type != "testappassist") { 208 _deps = [ ":module_${target_name}" ] 209 } else if (_project_type == "pythontest") { 210 print("this is pythontest") 211 } else if (_project_type != "testhapassist" && 212 _project_type != "testappassist") { 213 _deps = [] 214 } 215 _suite_out_dir = "${SUITES_OUTPUT_ROOT}/${XTS_SUITENAME}" 216 217 _archive_testfile = "" 218 if (_project_type == "testhapassist" || _project_type == "testappassist") { 219 _archive_testfile = 220 "${_suite_out_dir}/testcases/module_${_archive_filename}" 221 } else if (_project_type == "testhap") { 222 _hap_name = invoker.hap_name 223 if (_hap_name == "validator") { 224 _archive_testfile = 225 "${SUITES_OUTPUT_ROOT}/acts-validator/testcases/${_archive_filename}" 226 } else { 227 _archive_testfile = "${_suite_out_dir}/testcases/${_archive_filename}" 228 } 229 } else { 230 _archive_testfile = "${_suite_out_dir}/testcases/${_archive_filename}" 231 } 232 _arguments = [ 233 "build_module_with_testbundle", 234 "--build_gen_dir", 235 rebase_path("$root_gen_dir"), 236 "--build_target_name", 237 target_name, 238 "--buildgen_testfile", 239 rebase_path(_output_file), 240 "--project_path", 241 rebase_path("."), 242 "--test_xml", 243 rebase_path(TESTCONFIG_FILENAME), 244 "--project_type", 245 _project_type, 246 "--suite_out_dir", 247 rebase_path("${_suite_out_dir}"), 248 "--archive_testfile", 249 rebase_path("${_archive_testfile}"), 250 ] 251 252 if (_subsystem_name != "") { 253 _arguments += [ 254 "--subsystem_name", 255 _subsystem_name, 256 ] 257 } 258 if (_part_name != "") { 259 _arguments += [ 260 "--part_name", 261 _part_name, 262 ] 263 } 264 if (_apilibrary_deps != "") { 265 _arguments += [ 266 "--apilibrary_deps", 267 _apilibrary_deps, 268 ] 269 } 270 271 if (_test_files != "") { 272 _arguments += [ 273 "--test_files", 274 _test_files, 275 ] 276 } 277 if (_build_part_boolean == true && _project_type != "testhapassist" && 278 _project_type != "testappassist") { 279 action(target_name) { 280 deps = _deps 281 script = rebase_path("//test/xts/tools/build/suite.py") 282 args = _arguments 283 outputs = [ _archive_testfile ] 284 testonly = true 285 } 286 } else if (_build_part_boolean == false) { 287 action(target_name) { 288 script = rebase_path("//test/xts/tools/build/judgePart.py") 289 args = _arguments 290 outputs = [ _archive_testfile ] 291 testonly = true 292 } 293 if (defined(_deps)) { 294 print(_deps) 295 } 296 } 297} 298 299template("ohos_moduletest_suite") { 300 target("ohos_testsuite_base", "${target_name}") { 301 forward_variables_from(invoker, "*") 302 if (!defined(module_out_path)) { 303 module_out_path = "xts/modules" 304 } 305 project_type = "gtest" 306 } 307} 308 309template("ohos_hap_suite") { 310 target("ohos_testsuite_base", "${target_name}") { 311 forward_variables_from(invoker, "*") 312 313 # auto add HJUnitRunner entry ability and test-framework 314 if (defined(hap_profile)) { 315 # NOTE:: the GN tool disallow source files located in the ${out_dir} 316 # so we put the generated files in the xts dir. remember to REMOVE these. 317 _profile_relative_path = rebase_path(hap_profile, rebase_path(XTS_ROOT)) 318 _fixed_profile_path = 319 "${XTS_ROOT}/autogen_apiobjs/${_profile_relative_path}" 320 _fixer_script = rebase_path( 321 "//test/xts/tools/build/adapter/haptest_manifest_fixer.py") 322 exec_script(_fixer_script, 323 [ 324 "add_entryability", 325 "--raw_file=" + rebase_path(hap_profile), 326 "--dest_file=" + rebase_path(_fixed_profile_path), 327 ]) 328 hap_profile = _fixed_profile_path 329 } 330 331 if (defined(deps)) { 332 deps += [ "//test/xts/tools/hjunit:testkit_harmonyjunitrunner_java" ] 333 } else { 334 deps = [ "//test/xts/tools/hjunit:testkit_harmonyjunitrunner_java" ] 335 } 336 337 project_type = "testhap" 338 } 339} 340 341template("ohos_js_hap_suite") { 342 target("ohos_testsuite_base", "${target_name}") { 343 forward_variables_from(invoker, "*") 344 project_type = "testhap" 345 346 #js_build_mode = "debug" 347 } 348} 349 350template("ohos_js_app_suite") { 351 target("ohos_testsuite_base", "${target_name}") { 352 forward_variables_from(invoker, "*") 353 project_type = "testapp" 354 } 355} 356 357template("ohos_shell_app_suite") { 358 target("ohos_adapter_shell_app_suite", "${target_name}") { 359 forward_variables_from(invoker, "*") 360 } 361} 362 363template("ohos_prebuilt_suite") { 364 assert(!defined(invoker.source_files) || !defined(invoker.jar_path) || 365 !defined(invoker.source_dir), 366 "source_files, jar_path or source_dir must be specified one.") 367 assert(!defined(invoker.final_jar_path), 368 "final_jar_path is not allowed in target ${target_name}") 369 370 if (defined(invoker.jar_path)) { 371 _output_name = "${target_name}.jar" 372 if (defined(invoker.output_name)) { 373 _output_name = "${invoker.output_name}.jar" 374 } 375 376 _output_type = "tools" 377 if (defined(invoker.output_type)) { 378 _output_type = invoker.output_type 379 } 380 381 _final_jar_path = 382 "${SUITES_OUTPUT_ROOT}/${XTS_SUITENAME}/${_output_type}/${_output_name}" 383 384 target("java_prebuilt", "${target_name}") { 385 forward_variables_from(invoker, "*") 386 final_jar_path = _final_jar_path 387 is_host_library = true 388 } 389 } else { 390 assert(defined(invoker.output_dir), 391 "output_dir is require in target ${target_name}") 392 _outputs = [] 393 _copy_args = [ 394 "--method_name", 395 "copy_file", 396 ] 397 _deps = [] 398 _output_dir = "${SUITES_OUTPUT_ROOT}/${XTS_SUITENAME}/${invoker.output_dir}" 399 if (defined(invoker.source_dir)) { 400 _copy_args += [ 401 "--arguments", 402 "output=" + rebase_path(_output_dir) + "#source_dirs=" + 403 rebase_path(invoker.source_dir) + "#to_dir=True", 404 ] 405 _outputs = [ _output_dir ] 406 } else if (defined(invoker.source_files)) { 407 _sources = "" 408 foreach(src, invoker.source_files) { 409 _sources = _sources + rebase_path(src) + "," 410 } 411 _copy_args += [ 412 "--arguments", 413 "output=" + rebase_path(_output_dir) + "#sources=" + _sources + 414 "#to_dir=True", 415 ] 416 _outputs = [ _output_dir ] 417 } 418 if (defined(invoker.deps)) { 419 _deps = invoker.deps 420 } 421 action(target_name) { 422 script = rebase_path("//test/xts/tools/build/utils.py") 423 deps = _deps 424 args = _copy_args 425 outputs = _outputs 426 } 427 } 428} 429 430template("ohos_deploy_xdevice") { 431 _suite_out_dir = "${SUITES_OUTPUT_ROOT}/${XTS_SUITENAME}" 432 433 _args = [ 434 "build_xdevice", 435 "--source_dir", 436 rebase_path("//test/testfwk/xdevice"), 437 "--suite_out_dir", 438 rebase_path(_suite_out_dir), 439 ] 440 441 if (defined(invoker.configs_dir)) { 442 _args += [ 443 "--configs_dir", 444 rebase_path(rebase_path(invoker.configs_dir)), 445 ] 446 } 447 448 if (defined(invoker.resources_dir)) { 449 _args += [ 450 "--resources_dir", 451 rebase_path(rebase_path(invoker.resources_dir)), 452 ] 453 } 454 455 action(target_name) { 456 testonly = true 457 script = rebase_path("//test/xts/tools/build/suite.py") 458 args = _args 459 outputs = [ 460 "${_suite_out_dir}/tools/xdevice-ohos-0.0.0.tar.gz", 461 "${_suite_out_dir}/tools/xdevice-0.0.0.tar.gz", 462 "${_suite_out_dir}/tools/run.sh", 463 "${_suite_out_dir}/tools/run.bat", 464 ] 465 } 466} 467 468template("ohos_test_suite") { 469 _output = "${SUITES_OUTPUT_ROOT}/${target_name}.zip" 470 _suite_path = rebase_path("${SUITES_OUTPUT_ROOT}/${XTS_SUITENAME}") 471 _suite_archive_dir = rebase_path("${SUITE_ARCHIVE_DIR}") 472 _prebuilts_files = rebase_path("//xts/resource") 473 _gen_args = [ 474 "archive_suite", 475 "--suite_path", 476 _suite_path, 477 "--prebuilts_resource", 478 _prebuilts_files, 479 "--suite_archive_dir", 480 _suite_archive_dir, 481 "--make_archive", 482 "${make_archive}", 483 ] 484 485 _deps = [] 486 if (defined(invoker.deps)) { 487 _deps += invoker.deps 488 } 489 490 action(target_name) { 491 testonly = true 492 script = rebase_path("//test/xts/tools/build/suite.py") 493 deps = _deps 494 args = _gen_args 495 outputs = [ _output ] 496 } 497} 498 499template("pythontest_suite") { 500 assert(defined(invoker.script), "script is required in target ${target_name}") 501 502 _subsystem_name = "" 503 if (defined(invoker.subsystem_name)) { 504 _subsystem_name = invoker.subsystem_name 505 } else { 506 _local_path = rebase_path(".") 507 _args1 = [ 508 "--method_name", 509 "get_subsystem_name", 510 "--arguments", 511 "path=${_local_path}", 512 ] 513 _subsystem_name = 514 exec_script(rebase_path("//test/xts/tools/build/utils.py"), 515 _args1, 516 "trim string") 517 } 518 _deps = [] 519 if (defined(invoker.deps)) { 520 _deps = invoker.deps + _deps 521 } 522 _outputs_dir = "" 523 if (defined(invoker.outputs_dir)) { 524 _outputs_dir = invoker.outputs_dir 525 } 526 _output_file = rebase_path("${invoker.script}") 527 528 target("ohos_testsuite_base", "${target_name}") { 529 project_type = "pythontest" 530 subsystem_name = _subsystem_name 531 output_file = _output_file 532 deps = _deps 533 outputs_dir = _outputs_dir 534 } 535} 536 537template("executable_suite") { 538 assert(defined(invoker.suite_name), 539 "suite_name is required in target ${target_name}") 540 _suite_name = invoker.suite_name 541 _local_path = rebase_path(".") 542 _args1 = [ 543 "--method_name", 544 "get_subsystem_name", 545 "--arguments", 546 "path=${_local_path}", 547 ] 548 _subsystem_name = exec_script(rebase_path("//test/xts/tools/build/utils.py"), 549 _args1, 550 "trim string") 551 552 _outputs_dir = "" 553 if (defined(invoker.outputs_dir)) { 554 _outputs_dir = "${invoker.outputs_dir}" 555 } 556 ohos_executable(target_name) { 557 forward_variables_from(invoker, 558 "*", 559 [ 560 "test_type", 561 "module_out_path", 562 "visibility", 563 ]) 564 forward_variables_from(invoker, [ "visibility" ]) 565 if (!defined(deps)) { 566 deps = [] 567 } 568 569 subsystem_name = "tests" 570 part_name = "ssts" 571 ohos_test = true 572 testonly = true 573 output_name = "$target_name" 574 test_output_dir = "$SUITES_OUTPUT_ROOT/${_suite_name}/testcases/${_subsystem_name}/${_outputs_dir}" 575 if (defined(invoker.output_extension)) { 576 output_extension = invoker.output_extension 577 } 578 } 579} 580 581template("js_hap_suite") { 582 assert(defined(invoker.hap_source_path), 583 "hap_source_path is required in target ${target_name}") 584 assert(defined(invoker.test_hap_name), 585 "test_hap_name is required in target ${target_name}") 586 if (defined(invoker.deps)) { 587 _deps = invoker.deps 588 } 589 target("ohos_testsuite_base", "${target_name}") { 590 forward_variables_from(invoker, "*") 591 project_type = "js_test_hap" 592 } 593} 594 595template("ohos_hap_assist_suite") { 596 target("ohos_testsuite_base", target_name) { 597 forward_variables_from(invoker, "*") 598 project_type = "testhapassist" 599 } 600} 601 602template("ohos_app_assist_suite") { 603 target("ohos_testsuite_base", target_name) { 604 forward_variables_from(invoker, "*") 605 project_type = "testappassist" 606 } 607} 608