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