• 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/config/clang/clang.gni")
15import("//build/config/python.gni")
16import("//build/ohos.gni")
17import("//build/ohos_var.gni")
18import("//build/templates/cxx/cxx.gni")
19
20template("_testcase_resources") {
21  assert(defined(invoker.testcase_target_name))
22  assert(defined(invoker.test_output_dir))
23  assert(defined(invoker.module_out_path))
24
25  _deps = []
26  if (defined(invoker.deps)) {
27    _deps += invoker.deps
28  }
29  action_with_pydeps(target_name) {
30    if (defined(invoker.testonly)) {
31      testonly = invoker.testonly
32    }
33    deps = _deps
34    inputs = []
35    script = "//build/ohos/testfwk/testcase_resource_copy.py"
36    output_file = "$target_out_dir/$target_name.json"
37    outputs = [ output_file ]
38    depfile = "$target_gen_dir/$target_name.d"
39    args = []
40    if (defined(invoker.resource_config_file)) {
41      args += [
42        "--resource-config-file",
43        rebase_path(invoker.resource_config_file, root_build_dir),
44      ]
45      inputs += [ invoker.resource_config_file ]
46    }
47    args += [
48      "--depfile",
49      rebase_path(depfile, root_build_dir),
50      "--testcase-target-name",
51      invoker.testcase_target_name,
52      "--part-build-out-path",
53      rebase_path(root_out_dir, root_build_dir),
54      "--resource-output-path",
55      rebase_path(invoker.test_output_dir + "/resource", root_build_dir),
56      "--module-out-path",
57      invoker.module_out_path,
58      "--output-file",
59      rebase_path(output_file, root_build_dir),
60    ]
61  }
62}
63
64# ohos test template
65template("_ohos_test") {
66  assert(defined(invoker.test_type), "test_type is required.")
67  assert(defined(invoker.module_out_path))
68
69  _deps = []
70  if (defined(invoker.deps)) {
71    _deps += invoker.deps
72  }
73
74  test_output_dir =
75      "$root_out_dir/tests/${invoker.test_type}/${invoker.module_out_path}"
76
77  # generate module list file in gn stage
78  if (!skip_generate_module_list_file) {
79    _gen_module_list_script = "//build/ohos/testfwk/gen_module_list_files.py"
80    _arguments = [
81      "--target",
82      target_name,
83      "--target_label",
84      get_label_info(":$target_name", "label_with_toolchain"),
85      "--source_dir",
86      rebase_path(get_label_info(":$target_name", "dir"), root_build_dir),
87      "--test_type",
88      invoker.test_type,
89    ]
90
91    _module_list_file = "$root_out_dir/module_list_files/${invoker.module_out_path}/$target_name.mlf"
92
93    _arguments += [
94      "--output_dir",
95      rebase_path(test_output_dir, root_build_dir),
96      "--module_list_file",
97      rebase_path(_module_list_file, root_build_dir),
98    ]
99    exec_script(_gen_module_list_script, _arguments)
100  }
101
102  # copy testcase resource
103  testcase_target_name = target_name
104  _testcase_resources("${testcase_target_name}_resource_copy") {
105    if (defined(invoker.resource_config_file)) {
106      resource_config_file = invoker.resource_config_file
107    }
108    module_out_path = invoker.module_out_path
109    deps = _deps
110    testonly = true
111  }
112
113  # copy fuzz config file
114  if (defined(invoker.fuzz_config_file)) {
115    fuzz_config_file = invoker.fuzz_config_file
116    script = "//build/ohos/testfwk/fuzz_config_file_copy.py"
117    _arguments = []
118    _arguments += [
119      "--fuzz-config-file-path",
120      rebase_path(fuzz_config_file, root_build_dir),
121      "--fuzz-config-file-output-path",
122      rebase_path(root_out_dir + "/tests/res", root_build_dir),
123    ]
124    exec_script(script, _arguments)
125  }
126
127  _has_sources = defined(invoker.sources) && invoker.sources != []
128  if (_has_sources) {
129    _c_sources_file = "$target_gen_dir/$target_name.sources"
130    write_file(_c_sources_file, rebase_path(invoker.sources, root_build_dir))
131  }
132
133  ohos_executable(target_name) {
134    forward_variables_from(invoker,
135                           "*",
136                           [
137                             "test_type",
138                             "module_out_path",
139                             "visibility",
140                           ])
141    forward_variables_from(invoker, [ "visibility" ])
142    if (!defined(deps)) {
143      deps = []
144    }
145    deps += [ ":${testcase_target_name}_resource_copy" ]
146
147    subsystem_name = "tests"
148    part_name = invoker.test_type
149    ohos_test = true
150    testonly = true
151    output_name = "$target_name"
152  }
153}
154
155template("ohos_unittest") {
156  _ohos_test(target_name) {
157    forward_variables_from(invoker, "*")
158    test_type = "unittest"
159    deps = []
160    if (defined(invoker.deps)) {
161      deps += invoker.deps
162    }
163    deps += [ "//third_party/googletest:gtest_main" ]
164  }
165}
166
167template("ohos_moduletest") {
168  _ohos_test(target_name) {
169    forward_variables_from(invoker, "*")
170    test_type = "moduletest"
171    deps = []
172    if (defined(invoker.deps)) {
173      deps += invoker.deps
174    }
175    deps += [ "//third_party/googletest:gtest_main" ]
176  }
177}
178
179template("ohos_systemtest") {
180  _ohos_test(target_name) {
181    forward_variables_from(invoker, "*")
182    test_type = "systemtest"
183  }
184}
185
186template("ohos_performancetest") {
187  _ohos_test(target_name) {
188    forward_variables_from(invoker, "*")
189    test_type = "performance"
190    deps = []
191    if (defined(invoker.deps)) {
192      deps += invoker.deps
193    }
194    deps += [
195      "//test/testfwk/developer_test/aw/cxx/hwext:performance_test_static",
196      "//third_party/googletest:gtest_main",
197    ]
198  }
199}
200
201template("ohos_securitytest") {
202  _ohos_test(target_name) {
203    forward_variables_from(invoker, "*")
204    test_type = "security"
205  }
206}
207
208template("ohos_reliabilitytest") {
209  _ohos_test(target_name) {
210    forward_variables_from(invoker, "*")
211    test_type = "reliability"
212  }
213}
214
215template("ohos_distributedtest") {
216  _ohos_test(target_name) {
217    forward_variables_from(invoker, "*")
218    test_type = "distributedtest"
219    deps = []
220    if (defined(invoker.deps)) {
221      deps += invoker.deps
222    }
223    deps += [
224      "//test/testfwk/developer_test/aw/cxx/distributed:distributedtest_lib",
225    ]
226  }
227}
228
229template("ohos_fuzztest") {
230  # Judge the compliation library.
231  # Do the FUZZ compliation if the compliation library is the musl.
232  if (use_musl) {
233    _ohos_test(target_name) {
234      forward_variables_from(invoker,
235                             "*",
236                             [
237                               "deps",
238                               "cflags",
239                             ])
240      test_type = "fuzztest"
241      deps = []
242      if (defined(invoker.deps)) {
243        deps += invoker.deps
244      }
245      cflags = []
246      if (defined(invoker.cflags)) {
247        cflags += invoker.cflags
248      }
249      cflags += [
250        "-fno-sanitize-coverage=trace-pc-guard,edge,trace-cmp,indirect-calls,8bit-counters",
251        "-fsanitize=fuzzer",
252      ]
253      if (target_cpu == "arm64") {
254        libs =
255            [ "$clang_lib_base_path/aarch64-linux-ohos/libclang_rt.fuzzer.a" ]
256      } else if (target_cpu == "x86_64") {
257        libs = [ "$clang_lib_base_path/x86_64-linux-ohos/libclang_rt.fuzzer.a" ]
258      } else {
259        libs = [ "$clang_lib_base_path/arm-linux-ohos/libclang_rt.fuzzer.a" ]
260      }
261    }
262  } else {
263    group(target_name) {
264      not_needed(invoker, "*")
265    }
266  }
267}
268
269template("ohos_benchmarktest") {
270  _ohos_test(target_name) {
271    forward_variables_from(invoker, "*", [ "deps" ])
272    test_type = "benchmark"
273
274    deps = []
275    if (defined(invoker.deps)) {
276      deps += invoker.deps
277    }
278    deps += [ "//third_party/benchmark:benchmark" ]
279  }
280}
281
282template("_test_py_file_copy") {
283  assert(defined(invoker.sources), "sources is required.")
284  assert(defined(invoker.target_base_dir))
285  assert(defined(invoker.copy_output_dir))
286
287  action_with_pydeps(target_name) {
288    forward_variables_from(invoker,
289                           [
290                             "sources",
291                             "testonly",
292                             "visibility",
293                           ])
294    script = "//build/ohos/testfwk/test_py_file_copy.py"
295    deps = []
296    if (defined(invoker.deps)) {
297      deps += invoker.deps
298    }
299
300    depfile = "$target_gen_dir/$target_name.d"
301    outfile = "$target_out_dir/$target_name.out"
302    outputs = [ outfile ]
303    args = [
304      "--target-base-dir",
305      rebase_path(invoker.target_base_dir, root_build_dir),
306      "--copy-output-dir",
307      rebase_path(invoker.copy_output_dir, root_build_dir),
308      "--outfile",
309      rebase_path(outfile, root_build_dir),
310      "--depfile",
311      rebase_path(depfile, root_build_dir),
312      "--source-files",
313    ]
314    args += rebase_path(sources, root_build_dir)
315  }
316}
317
318# python test template
319template("_ohos_test_py") {
320  assert(defined(invoker.test_type), "test_type is required.")
321  assert(defined(invoker.sources), "sources is required.")
322
323  _gen_module_list_script = "//build/ohos/testfwk/gen_module_list_files.py"
324  _arguments = [
325    "--target",
326    target_name,
327    "--target_label",
328    get_label_info(":$target_name", "label_with_toolchain"),
329    "--source_dir",
330    rebase_path(get_label_info(":$target_name", "dir"), root_build_dir),
331    "--test_type",
332    invoker.test_type,
333  ]
334
335  if (defined(invoker.module_out_path)) {
336    test_output_dir =
337        "$root_out_dir/tests/${invoker.test_type}/${invoker.module_out_path}"
338    _module_list_file = "$root_out_dir/module_list_files/${invoker.module_out_path}/$target_name.mlf"
339  } else {
340    test_output_dir = "$root_out_dir/tests/${invoker.test_type}"
341    _module_list_file = "$root_out_dir/module_list_files/$target_name.mlf"
342  }
343
344  _arguments += [
345    "--output_dir",
346    rebase_path(test_output_dir, root_build_dir),
347    "--module_list_file",
348    rebase_path(_module_list_file, root_build_dir),
349  ]
350  exec_script(_gen_module_list_script, _arguments)
351
352  _test_py_file_copy(target_name) {
353    testonly = true
354    target_base_dir = get_label_info(":$target_name", "dir")
355    copy_output_dir = test_output_dir
356    sources = get_path_info(invoker.sources, "abspath")
357  }
358}
359
360template("ohos_unittest_py") {
361  _ohos_test_py(target_name) {
362    forward_variables_from(invoker, "*")
363    test_type = "unittest"
364  }
365}
366
367template("ohos_moduletest_py") {
368  _ohos_test_py(target_name) {
369    forward_variables_from(invoker, "*")
370    test_type = "moduletest"
371  }
372}
373
374template("ohos_systemtest_py") {
375  _ohos_test_py(target_name) {
376    forward_variables_from(invoker, "*")
377    test_type = "systemtest"
378  }
379}
380
381template("ohos_performancetest_py") {
382  _ohos_test_py(target_name) {
383    forward_variables_from(invoker, "*")
384    test_type = "performance"
385  }
386}
387
388template("ohos_securitytest_py") {
389  _ohos_test_py(target_name) {
390    forward_variables_from(invoker, "*")
391    test_type = "security"
392  }
393}
394
395template("ohos_reliabilitytest_py") {
396  _ohos_test_py(target_name) {
397    forward_variables_from(invoker, "*")
398    test_type = "reliability"
399  }
400}
401
402template("ohos_distributedtest_py") {
403  _ohos_test_py(target_name) {
404    forward_variables_from(invoker, "*")
405    test_type = "distributedtest"
406  }
407}
408
409template("ohos_fuzztest_py") {
410  _ohos_test_py(target_name) {
411    forward_variables_from(invoker, "*")
412    test_type = "fuzztest"
413  }
414}
415
416#js api test template
417template("ohos_js_test") {
418  assert(defined(invoker.test_type), "test_type must be defined.")
419  assert(defined(invoker.hap_profile), "hap_profile must be defined.")
420  assert(defined(invoker.module_out_path), "module_out_path must be defined.")
421
422  # generate module list file in gn stage
423  _gen_module_list_script = "//build/ohos/testfwk/gen_module_list_files.py"
424  _arguments = [
425    "--target",
426    target_name,
427    "--target_label",
428    get_label_info(":$target_name", "label_with_toolchain"),
429    "--source_dir",
430    rebase_path(get_label_info(":$target_name", "dir"), root_build_dir),
431    "--test_type",
432    invoker.test_type,
433  ]
434
435  _test_output_dir =
436      "$root_out_dir/tests/${invoker.test_type}/${invoker.module_out_path}"
437  _module_list_file = "$root_out_dir/module_list_files/${invoker.module_out_path}/$target_name.mlf"
438
439  _arguments += [
440    "--output_dir",
441    rebase_path(_test_output_dir, root_build_dir),
442    "--module_list_file",
443    rebase_path(_module_list_file, root_build_dir),
444  ]
445  exec_script(_gen_module_list_script, _arguments)
446
447  # copy testcase resource
448  testcase_target_name = target_name
449  _testcase_resources("${testcase_target_name}_resource_copy") {
450    if (defined(invoker.resource_config_file)) {
451      resource_config_file = invoker.resource_config_file
452    }
453    module_out_path = invoker.module_out_path
454    test_output_dir = _test_output_dir
455  }
456
457  _suite_path = get_label_info(":$target_name", "dir")
458  _template_path = "//test/testfwk/developer_test/libs/js_template"
459  _target_path = "${_template_path}/$target_name"
460  _target_js_copy = "${target_name}__js_copy"
461  action_with_pydeps(_target_js_copy) {
462    script = "//build/ohos/testfwk/test_js_file_copy.py"
463    outputs = [ "$target_out_dir/$target_name.out" ]
464    args = [
465      "--suite_path",
466      rebase_path(_suite_path, root_build_dir),
467      "--template_path",
468      rebase_path(_template_path, root_build_dir),
469      "--target_path",
470      rebase_path(_target_path, root_build_dir),
471    ]
472    deps = [ ":${testcase_target_name}_resource_copy" ]
473  }
474
475  _target_js_assets = "${target_name}__intl_js_assets"
476  ohos_js_assets(_target_js_assets) {
477    source_dir = "${_target_path}/src/main/js/default"
478    deps = [ ":$_target_js_copy" ]
479  }
480  _target_resources = "${target_name}__resources"
481  ohos_resources(_target_resources) {
482    sources = [ "${_target_path}/src/main/resources" ]
483    deps = [ ":$_target_js_assets" ]
484    hap_profile = invoker.hap_profile
485  }
486
487  ohos_hap(target_name) {
488    forward_variables_from(invoker,
489                           "*",
490                           [
491                             "test_type",
492                             "module_out_path",
493                             "visibility",
494                           ])
495    forward_variables_from(invoker, [ "visibility" ])
496
497    deps = [
498      ":$_target_js_assets",
499      ":$_target_resources",
500    ]
501    final_hap_path = "$_test_output_dir/$target_name.hap"
502    js_build_mode = "debug"
503    testonly = true
504  }
505}
506
507template("ohos_js_unittest") {
508  ohos_js_test(target_name) {
509    forward_variables_from(invoker, "*")
510    test_type = "unittest"
511  }
512}
513