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