0

autotest.py: Support policy to pref mappings tests

Pref mapping test files now work, for example:
$ tools/autotest.py -C out/Default \
    components/policy/test/data/pref_mapping/NewWindowsInKioskAllowed.json
$ tools/autotest.py -C out/Default \
    components/policy/test/data/pref_mapping \
    --test_policy_to_pref_mappings_filter=NewWindowsInKioskAllowed

Which are equivalent to:
$ autoninja -C out/Default browser_tests
$ out/Default/browser_tests \
    --gtest_filter="*PolicyPrefsTest.PolicyToPrefsMapping" \
    --test_policy_to_pref_mappings_filter="NewWindowsInKioskAllowed"

Bug: b:319254568
Test: tools/autotest.py -C out/Default components/policy/test/data/pref_mapping
Test: tools/autotest.py -C out/Default components/policy/test/data/pref_mapping/NewWindowsInKioskAllowed.json
Change-Id: I8eb36a69abd717bf69c56a7f1aa453209c3da5a2
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/5185801
Reviewed-by: Maksim Ivanov <emaxx@chromium.org>
Reviewed-by: Michael Thiessen <mthiesse@chromium.org>
Commit-Queue: Edman Anjos <edman@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1254565}
This commit is contained in:
Edman Anjos
2024-01-31 17:02:27 +00:00
committed by Chromium LUCI CQ
parent cc18b1039b
commit e7481c5056
2 changed files with 67 additions and 14 deletions

@ -29,19 +29,39 @@ such a test is missing (see `reason_for_missing_test`).
## Running the tests
To run the preference mapping tests, use `browser_tests` command with a
`--gtest_filter` for one of the following
The simplest way to run preference mapping tests is to use `tools/autotest.py`
and list the files you want to test. For example:
```bash
$ testing/xvfb.py tools/autotest.py -C out/Default \
components/policy/test/data/pref_mapping/CrostiniAllowed.json \
components/policy/test/data/pref_mapping/CrostiniPortForwardingAllowed.json
```
To do the same manually, build and run `browser_tests` with `--gtest_filter`
set to one of the following:
- `PolicyPrefsTestCoverageTest.AllPoliciesHaveATestCase`
- `PolicyPrefsTest.PolicyToPrefsMapping`
- `*ChunkedPolicyPrefsTest.PolicyToPrefsMapping*`
- `SigninPolicyPrefsTest.PolicyToPrefsMapping` (CrOS only)
- `PolicyTest.AllPoliciesHaveATestCase` (iOS only)
- `PolicyTest.PolicyToPrefMappings` (iOS only)
Individual policies for PolicyPrefsTest.PolicyToPrefsMapping could be filtered
by `--test_policy_to_pref_mappings_filter` flag. The flag accepts policy names
Individual policies in `ChunkedPolicyPrefsTest` can be filtered with the
`--test_policy_to_pref_mappings_filter` flag. The flag accepts policy names
(with the .optionalTestNameSuffix) separated by colon.
```bash
$ autoninja -C out/Default browser_tests
$ testing/xvfb.py out/Default/browser_tests \
--gtest_filter="*ChunkedPolicyPrefsTest.PolicyToPrefsMapping*" \
--test_policy_to_pref_mappings_filter="CrostiniAllowed:CrostiniPortForwardingAllowed"
```
There are also iOS only policy pref mapping `unit_tests`. To run them, execute
the test binary with the `--gtest_filter` set to one of:
- `PolicyTest.AllPoliciesHaveATestCase`
- `PolicyTest.PolicyToPrefMappings`
## Example
The following example tests the `IdleAction` policy, i.e. its mapping to two

@ -65,7 +65,12 @@ _TEST_TARGET_ALLOWLIST = [
_TEST_TARGET_REGEX = re.compile(
r'(_browsertests|_perftests|_wpr_tests|_unittests)$')
TEST_FILE_NAME_REGEX = re.compile(r'(.*Test\.java)|(.*_[a-z]*test\.cc)')
_PREF_MAPPING_FILE_PATTERN = re.escape(
str(Path('components') / 'policy' / 'test' / 'data' / 'pref_mapping') +
r'/') + r'.*\.json'
TEST_FILE_NAME_REGEX = re.compile(r'(.*Test\.java)|(.*_[a-z]*test\.cc)' +
r'|(' + _PREF_MAPPING_FILE_PATTERN + r')')
# Some tests don't directly include gtest.h and instead include it via gmock.h
# or a test_utils.h file, so make sure these cases are captured. Also include
@ -410,8 +415,9 @@ def FindTestTargets(target_cache, out_dir, paths, run_all):
return (test_targets, used_cache)
def RunTestTargets(out_dir, targets, gtest_filter, extra_args, dry_run,
no_try_android_wrappers, no_fast_local_dev):
def RunTestTargets(out_dir, targets, gtest_filter, pref_mapping_filter,
extra_args, dry_run, no_try_android_wrappers,
no_fast_local_dev):
for target in targets:
target_binary = target.split(':')[1]
@ -426,7 +432,11 @@ def RunTestTargets(out_dir, targets, gtest_filter, extra_args, dry_run,
# Usually want this flag when developing locally.
extra_args = extra_args + ['--fast-local-dev']
cmd = [path, f'--gtest_filter={gtest_filter}'] + extra_args
cmd = [path, f'--gtest_filter={gtest_filter}']
if pref_mapping_filter:
cmd.append(f'--test_policy_to_pref_mappings_filter={pref_mapping_filter}')
cmd.extend(extra_args)
print('Running test: ' + shlex.join(cmd))
if not dry_run:
StreamCommandOrExit(cmd)
@ -449,6 +459,12 @@ def BuildJavaTestFilter(filenames):
for f in filenames)
_PREF_MAPPING_GTEST_FILTER = '*PolicyPrefsTest.PolicyToPrefsMapping*'
SPECIAL_TEST_FILTERS = [(re.compile(_PREF_MAPPING_FILE_PATTERN),
_PREF_MAPPING_GTEST_FILTER)]
def BuildTestFilter(filenames, line):
java_files = [f for f in filenames if f.endswith('.java')]
cc_files = [f for f in filenames if f.endswith('.cc')]
@ -457,10 +473,18 @@ def BuildTestFilter(filenames, line):
filters.append(BuildJavaTestFilter(java_files))
if cc_files:
filters.append(BuildCppTestFilter(cc_files, line))
for regex, gtest_filter in SPECIAL_TEST_FILTERS:
if any(True for f in filenames if regex.match(f)):
filters.append(gtest_filter)
break
return ':'.join(filters)
def BuildPrefMappingTestFilter(filenames):
names_without_extension = [Path(f).stem for f in filenames]
return ':'.join(names_without_extension)
def main():
parser = argparse.ArgumentParser(
description=__doc__, formatter_class=argparse.RawTextHelpFormatter)
@ -484,6 +508,10 @@ def main():
'-f',
metavar='FILTER',
help='test filter')
parser.add_argument('--test_policy_to_pref_mappings_filter',
'--test-policy-to-pref-mappings-filter',
metavar='PREF_MAPPINGS_FILTER',
help='policy pref mappings test filter')
parser.add_argument(
'--dry-run',
'--dry_run',
@ -528,6 +556,10 @@ def main():
if not gtest_filter:
ExitWithMessage('Failed to derive a gtest filter')
pref_mapping_filter = args.test_policy_to_pref_mappings_filter
if not pref_mapping_filter:
pref_mapping_filter = BuildPrefMappingTestFilter(filenames)
assert targets
build_ok = BuildTestTargets(out_dir, targets, args.dry_run)
@ -547,8 +579,9 @@ def main():
if not build_ok: sys.exit(1)
RunTestTargets(out_dir, targets, gtest_filter, _extras, args.dry_run,
args.no_try_android_wrappers, args.no_fast_local_dev)
RunTestTargets(out_dir, targets, gtest_filter, pref_mapping_filter, _extras,
args.dry_run, args.no_try_android_wrappers,
args.no_fast_local_dev)
if __name__ == '__main__':