Yilin Yang | 8e1b24c | 2020-02-03 15:02:25 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python3 |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 2 | # |
| 3 | # Copyright 2015 The Chromium OS Authors. All rights reserved. |
| 4 | # Use of this source code is governed by a BSD-style license that can be |
| 5 | # found in the LICENSE file. |
| 6 | |
| 7 | """Tests for regions.py. |
| 8 | |
| 9 | These tests ensure that all regions in regions.py are valid. The tests use |
| 10 | testdata pulled from the Chromium sources. |
| 11 | """ |
| 12 | |
| 13 | from __future__ import print_function |
| 14 | |
Yilin Yang | 7b242ae | 2020-02-03 15:19:25 +0800 | [diff] [blame] | 15 | import io |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 16 | import os |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 17 | import unittest |
Hung-Te Lin | 98501ae | 2017-06-02 15:53:33 +0800 | [diff] [blame] | 18 | import logging |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 19 | |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 20 | import regions |
| 21 | import yaml |
| 22 | |
| 23 | |
Hung-Te Lin | 8f8e0c4 | 2015-04-13 11:42:42 +0800 | [diff] [blame] | 24 | _WARN_UNKNOWN_DATA_IN_UNCONFIRMED_REGION = ( |
| 25 | 'Missing %s %r; does this new data need to be added to CrOS, or ' |
| 26 | 'does testdata need to be updated? (just a warning, since region ' |
| 27 | '%r is not a confirmed region)') |
| 28 | |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 29 | class RegionTest(unittest.TestCase): |
| 30 | """Tests for the Region class.""" |
| 31 | |
| 32 | @classmethod |
| 33 | def _ReadTestData(cls, name): |
| 34 | """Reads a YAML-formatted test data file. |
| 35 | |
| 36 | Args: |
| 37 | name: Name of file in the testdata directory. |
| 38 | |
| 39 | Returns: |
| 40 | The parsed value. |
| 41 | """ |
| 42 | with open(os.path.join(os.path.dirname(__file__), |
| 43 | 'testdata', name + '.yaml')) as f: |
| 44 | return yaml.load(f) |
| 45 | |
| 46 | @classmethod |
| 47 | def setUpClass(cls): |
| 48 | cls.locales = cls._ReadTestData('locales') |
| 49 | cls.time_zones = cls._ReadTestData('time_zones') |
| 50 | cls.migration_map = cls._ReadTestData('migration_map') |
| 51 | cls.input_methods = cls._ReadTestData('input_methods') |
| 52 | |
| 53 | def _ResolveInputMethod(self, method): |
| 54 | """Resolves an input method using the migration map. |
| 55 | |
| 56 | Args: |
| 57 | method: An input method ID that may contain prefixes from the |
| 58 | migration map. (E.g., "m17n:ar", which contains the "m17n:" prefix.) |
| 59 | |
| 60 | Returns: |
| 61 | The input method ID after mapping any prefixes. (E.g., "m17n:ar" will |
| 62 | be mapped to "vkd_".) |
| 63 | """ |
| 64 | for k, v in self.migration_map: |
| 65 | if method.startswith(k): |
| 66 | method = v + method[len(k):] |
| 67 | return method |
| 68 | |
| 69 | def testZoneInfo(self): |
| 70 | all_regions = regions.BuildRegionsDict(include_all=True) |
| 71 | |
| 72 | # Make sure all time zones are present in /usr/share/zoneinfo. |
| 73 | all_zoneinfos = [os.path.join('/usr/share/zoneinfo', tz) |
| 74 | for r in all_regions.values() for tz in r.time_zones] |
| 75 | missing = [z for z in all_zoneinfos if not os.path.exists(z)] |
| 76 | self.assertFalse(missing, |
| 77 | ('Missing zoneinfo files; are timezones misspelled?: %r' % |
| 78 | missing)) |
| 79 | |
| 80 | def testBadLocales(self): |
Yilin Yang | 4d719b3 | 2020-02-03 15:11:08 +0800 | [diff] [blame] | 81 | self.assertRaisesRegex( |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 82 | AssertionError, "Locale 'en-us' does not match", regions.Region, |
| 83 | 'us', 'xkb:us::eng', 'America/Los_Angeles', 'en-us', 'ANSI') |
| 84 | |
| 85 | def testBadKeyboard(self): |
Yilin Yang | 4d719b3 | 2020-02-03 15:11:08 +0800 | [diff] [blame] | 86 | self.assertRaisesRegex( |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 87 | AssertionError, "Keyboard pattern 'xkb:us::' does not match", |
| 88 | regions.Region, 'us', 'xkb:us::', 'America/Los_Angeles', 'en-US', |
| 89 | 'ANSI') |
| 90 | |
| 91 | def testKeyboardRegexp(self): |
| 92 | self.assertTrue(regions.KEYBOARD_PATTERN.match('xkb:us::eng')) |
| 93 | self.assertTrue(regions.KEYBOARD_PATTERN.match('ime:ko:korean')) |
| 94 | self.assertTrue(regions.KEYBOARD_PATTERN.match('m17n:ar')) |
| 95 | self.assertFalse(regions.KEYBOARD_PATTERN.match('m17n:')) |
| 96 | self.assertFalse(regions.KEYBOARD_PATTERN.match('foo_bar')) |
| 97 | |
| 98 | def testTimeZones(self): |
| 99 | for r in regions.BuildRegionsDict(include_all=True).values(): |
| 100 | for tz in r.time_zones: |
| 101 | if tz not in self.time_zones: |
| 102 | if r.region_code in regions.REGIONS: |
| 103 | self.fail( |
Hung-Te Lin | 8f8e0c4 | 2015-04-13 11:42:42 +0800 | [diff] [blame] | 104 | 'Missing time zones: %r; does a new time zone need to be added ' |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 105 | 'to CrOS, or does testdata need to be updated?' % tz) |
| 106 | else: |
| 107 | # This is an unconfirmed region; just print a warning. |
Hung-Te Lin | 8f8e0c4 | 2015-04-13 11:42:42 +0800 | [diff] [blame] | 108 | logging.warn(_WARN_UNKNOWN_DATA_IN_UNCONFIRMED_REGION, 'time zone', |
| 109 | tz, r.region_code) |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 110 | |
| 111 | def testLocales(self): |
| 112 | missing = [] |
| 113 | for r in regions.BuildRegionsDict(include_all=True).values(): |
| 114 | for l in r.locales: |
| 115 | if l not in self.locales: |
Hung-Te Lin | 8f8e0c4 | 2015-04-13 11:42:42 +0800 | [diff] [blame] | 116 | if r.region_code in regions.REGIONS: |
| 117 | missing.append(l) |
| 118 | else: |
| 119 | logging.warn(_WARN_UNKNOWN_DATA_IN_UNCONFIRMED_REGION, 'locale', l, |
| 120 | r.region_code) |
| 121 | self.assertFalse(missing, |
| 122 | ('Missing locale; does testdata need to be updated?: %r' % |
| 123 | missing)) |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 124 | |
| 125 | def testInputMethods(self): |
| 126 | # Verify that every region is present in the dict. |
| 127 | for r in regions.BuildRegionsDict(include_all=True).values(): |
| 128 | for k in r.keyboards: |
| 129 | resolved_method = self._ResolveInputMethod(k) |
| 130 | # Make sure the keyboard method is present. |
Hung-Te Lin | 8f8e0c4 | 2015-04-13 11:42:42 +0800 | [diff] [blame] | 131 | if resolved_method not in self.input_methods: |
| 132 | if r.region_code in regions.REGIONS: |
| 133 | self.fail('Missing keyboard layout %r (resolved from %r)' % ( |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 134 | resolved_method, k)) |
Hung-Te Lin | 8f8e0c4 | 2015-04-13 11:42:42 +0800 | [diff] [blame] | 135 | else: |
| 136 | # This is an unconfirmed region; just print a warning. |
| 137 | logging.warn(_WARN_UNKNOWN_DATA_IN_UNCONFIRMED_REGION, 'keyboard', |
| 138 | k, r.region_code) |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 139 | |
| 140 | def testFirmwareLocales(self): |
Hung-Te Lin | 98501ae | 2017-06-02 15:53:33 +0800 | [diff] [blame] | 141 | # This file is probably in src/platform2/regions |
| 142 | src_root = os.environ.get('CROS_WORKON_SRCROOT', |
| 143 | os.path.join(os.path.dirname(__file__), '..', |
| 144 | '..', '..')) |
| 145 | bmpblk_dir = os.path.join(src_root, 'src', 'platform', 'bmpblk') |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 146 | if not os.path.exists(bmpblk_dir): |
| 147 | logging.warn('Skipping testFirmwareLocales, since %r is missing', |
| 148 | bmpblk_dir) |
| 149 | return |
| 150 | |
| 151 | bmp_locale_dir = os.path.join(bmpblk_dir, 'strings', 'locale') |
| 152 | for r in regions.BuildRegionsDict(include_all=True).values(): |
Marco Chen | 95a5a87 | 2019-05-13 23:52:56 +0800 | [diff] [blame] | 153 | checked_paths = [] |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 154 | for l in r.locales: |
| 155 | paths = [os.path.join(bmp_locale_dir, l)] |
| 156 | if '-' in l: |
| 157 | paths.append(os.path.join(bmp_locale_dir, l.partition('-')[0])) |
Marco Chen | 95a5a87 | 2019-05-13 23:52:56 +0800 | [diff] [blame] | 158 | if any([os.path.exists(x) for x in paths]): |
| 159 | break |
| 160 | checked_paths += paths |
| 161 | else: |
| 162 | if r.region_code in regions.REGIONS: |
| 163 | self.fail( |
| 164 | 'For region %r, none of %r exists' % (r.region_code, |
| 165 | checked_paths)) |
| 166 | else: |
| 167 | logging.warn('For region %r, none of %r exists; ' |
| 168 | 'just a warning since this region is not confirmed', |
| 169 | r.region_code, checked_paths) |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 170 | |
| 171 | def testYAMLOutput(self): |
Yilin Yang | 7b242ae | 2020-02-03 15:19:25 +0800 | [diff] [blame] | 172 | output = io.StringIO() |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 173 | regions.main(['--format', 'yaml'], output) |
| 174 | data = yaml.load(output.getvalue()) |
Yilin Yang | bb9e1ec | 2020-02-03 15:11:48 +0800 | [diff] [blame] | 175 | self.assertEqual( |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 176 | {'keyboards': ['xkb:us::eng'], |
| 177 | 'keyboard_mechanical_layout': 'ANSI', |
| 178 | 'locales': ['en-US'], |
| 179 | 'region_code': 'us', |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 180 | 'description': 'United States', |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 181 | 'regulatory_domain': 'US', |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 182 | 'time_zones': ['America/Los_Angeles']}, |
| 183 | data['us']) |
| 184 | |
| 185 | def testFieldsDict(self): |
| 186 | # 'description' and 'notes' should be missing. |
Yilin Yang | bb9e1ec | 2020-02-03 15:11:48 +0800 | [diff] [blame] | 187 | self.assertEqual( |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 188 | {'keyboards': ['xkb:b::b'], |
| 189 | 'keyboard_mechanical_layout': 'e', |
| 190 | 'description': 'description', |
| 191 | 'locales': ['d'], |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 192 | 'region_code': 'aa', |
| 193 | 'regulatory_domain': 'AA', |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 194 | 'time_zones': ['c']}, |
Hung-Te Lin | 98501ae | 2017-06-02 15:53:33 +0800 | [diff] [blame] | 195 | (regions.Region('aa', 'xkb:b::b', 'c', 'd', 'e', 'description', |
| 196 | 'notes').GetFieldsDict())) |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 197 | |
| 198 | def testConsolidateRegionsDups(self): |
| 199 | """Test duplicate handling. Two identical Regions are OK.""" |
| 200 | # Make two copies of the same region. |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 201 | region_list = [regions.Region('aa', 'xkb:b::b', 'c', 'd', 'e') |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 202 | for _ in range(2)] |
| 203 | # It's OK. |
Yilin Yang | bb9e1ec | 2020-02-03 15:11:48 +0800 | [diff] [blame] | 204 | self.assertEqual( |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 205 | {'aa': region_list[0]}, regions.ConsolidateRegions(region_list)) |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 206 | |
| 207 | # Modify the second copy. |
| 208 | region_list[1].keyboards = ['f'] |
| 209 | # Not OK anymore! |
Yilin Yang | 4d719b3 | 2020-02-03 15:11:08 +0800 | [diff] [blame] | 210 | self.assertRaisesRegex( |
Hung-Te Lin | 2c89ccd | 2015-04-07 15:20:35 +0800 | [diff] [blame] | 211 | regions.RegionException, "Conflicting definitions for region 'aa':", |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 212 | regions.ConsolidateRegions, region_list) |
| 213 | |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 214 | if __name__ == '__main__': |
Hung-Te Lin | 98501ae | 2017-06-02 15:53:33 +0800 | [diff] [blame] | 215 | logging.basicConfig(format='%(message)s', level=logging.WARNING) |
Hung-Te Lin | 76c55b2 | 2015-03-31 14:47:14 +0800 | [diff] [blame] | 216 | unittest.main() |