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