OLD | NEW |
1 #!/usr/bin/env python | 1 #!/usr/bin/env python |
2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 2 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
3 # Use of this source code is governed by a BSD-style license that can be | 3 # Use of this source code is governed by a BSD-style license that can be |
4 # found in the LICENSE file. | 4 # found in the LICENSE file. |
5 | 5 |
6 '''Unit tests for the 'grit build' tool. | 6 '''Unit tests for the 'grit build' tool. |
7 ''' | 7 ''' |
8 | 8 |
| 9 import codecs |
9 import os | 10 import os |
10 import sys | 11 import sys |
11 import tempfile | 12 import tempfile |
12 if __name__ == '__main__': | 13 if __name__ == '__main__': |
13 sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) | 14 sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) |
14 | 15 |
15 import unittest | 16 import unittest |
16 | 17 |
17 from grit import util | 18 from grit import util |
18 from grit.tool import build | 19 from grit.tool import build |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
78 self.failUnlessEqual(0, | 79 self.failUnlessEqual(0, |
79 builder_ok.Run(DummyOpts(), [ | 80 builder_ok.Run(DummyOpts(), [ |
80 '-o', output_dir, | 81 '-o', output_dir, |
81 '-a', os.path.abspath( | 82 '-a', os.path.abspath( |
82 os.path.join(output_dir, 'en_generated_resources.rc')), | 83 os.path.join(output_dir, 'en_generated_resources.rc')), |
83 '-a', os.path.abspath( | 84 '-a', os.path.abspath( |
84 os.path.join(output_dir, 'sv_generated_resources.rc')), | 85 os.path.join(output_dir, 'sv_generated_resources.rc')), |
85 '-a', os.path.abspath( | 86 '-a', os.path.abspath( |
86 os.path.join(output_dir, 'resource.h'))])) | 87 os.path.join(output_dir, 'resource.h'))])) |
87 | 88 |
| 89 def _verifyWhitelistedOutput(self, |
| 90 filename, |
| 91 whitelisted_ids, |
| 92 non_whitelisted_ids, |
| 93 encoding='utf8'): |
| 94 self.failUnless(os.path.exists(filename)) |
| 95 whitelisted_ids_found = [] |
| 96 non_whitelisted_ids_found = [] |
| 97 with codecs.open(filename, encoding=encoding) as f: |
| 98 for line in f.readlines(): |
| 99 for whitelisted_id in whitelisted_ids: |
| 100 if whitelisted_id in line: |
| 101 whitelisted_ids_found.append(whitelisted_id) |
| 102 for non_whitelisted_id in non_whitelisted_ids: |
| 103 if non_whitelisted_id in line: |
| 104 non_whitelisted_ids_found.append(non_whitelisted_id) |
| 105 self.longMessage = True |
| 106 self.assertEqual(whitelisted_ids, |
| 107 whitelisted_ids_found, |
| 108 '\nin file {}'.format(os.path.basename(filename))) |
| 109 non_whitelisted_msg = ('Non-Whitelisted IDs {} found in {}' |
| 110 .format(non_whitelisted_ids_found, os.path.basename(filename))) |
| 111 self.assertFalse(non_whitelisted_ids_found, non_whitelisted_msg) |
| 112 |
| 113 def testWhitelistStrings(self): |
| 114 output_dir = tempfile.mkdtemp() |
| 115 builder = build.RcBuilder() |
| 116 class DummyOpts(object): |
| 117 def __init__(self): |
| 118 self.input = util.PathFromRoot('grit/testdata/whitelist_strings.grd') |
| 119 self.verbose = False |
| 120 self.extra_verbose = False |
| 121 whitelist_file = util.PathFromRoot('grit/testdata/whitelist.txt') |
| 122 builder.Run(DummyOpts(), ['-o', output_dir, |
| 123 '-w', whitelist_file]) |
| 124 header = os.path.join(output_dir, 'whitelist_test_resources.h') |
| 125 rc = os.path.join(output_dir, 'en_whitelist_test_strings.rc') |
| 126 |
| 127 whitelisted_ids = ['IDS_MESSAGE_WHITELISTED'] |
| 128 non_whitelisted_ids = ['IDS_MESSAGE_NOT_WHITELISTED'] |
| 129 self._verifyWhitelistedOutput( |
| 130 header, |
| 131 whitelisted_ids, |
| 132 non_whitelisted_ids, |
| 133 ) |
| 134 self._verifyWhitelistedOutput( |
| 135 rc, |
| 136 whitelisted_ids, |
| 137 non_whitelisted_ids, |
| 138 encoding='utf16' |
| 139 ) |
| 140 |
| 141 def testWhitelistResources(self): |
| 142 output_dir = tempfile.mkdtemp() |
| 143 builder = build.RcBuilder() |
| 144 class DummyOpts(object): |
| 145 def __init__(self): |
| 146 self.input = util.PathFromRoot('grit/testdata/whitelist_resources.grd') |
| 147 self.verbose = False |
| 148 self.extra_verbose = False |
| 149 whitelist_file = util.PathFromRoot('grit/testdata/whitelist.txt') |
| 150 builder.Run(DummyOpts(), ['-o', output_dir, |
| 151 '-w', whitelist_file]) |
| 152 header = os.path.join(output_dir, 'whitelist_test_resources.h') |
| 153 map_cc = os.path.join(output_dir, 'whitelist_test_resources_map.cc') |
| 154 map_h = os.path.join(output_dir, 'whitelist_test_resources_map.h') |
| 155 pak = os.path.join(output_dir, 'whitelist_test_resources.pak') |
| 156 |
| 157 # Ensure the resource map header and .pak files exist, but don't verify |
| 158 # their content. |
| 159 self.failUnless(os.path.exists(map_h)) |
| 160 self.failUnless(os.path.exists(pak)) |
| 161 |
| 162 whitelisted_ids = [ |
| 163 'IDR_STRUCTURE_WHITELISTED', |
| 164 'IDR_STRUCTURE_IN_TRUE_IF_WHITELISTED', |
| 165 'IDR_INCLUDE_WHITELISTED', |
| 166 ] |
| 167 non_whitelisted_ids = [ |
| 168 'IDR_STRUCTURE_NOT_WHITELISTED', |
| 169 'IDR_STRUCTURE_IN_TRUE_IF_NOT_WHITELISTED', |
| 170 'IDR_STRUCTURE_IN_FALSE_IF_WHITELISTED', |
| 171 'IDR_STRUCTURE_IN_FALSE_IF_NOT_WHITELISTED', |
| 172 'IDR_INCLUDE_NOT_WHITELISTED', |
| 173 ] |
| 174 for output_file in (header, map_cc): |
| 175 self._verifyWhitelistedOutput( |
| 176 output_file, |
| 177 whitelisted_ids, |
| 178 non_whitelisted_ids, |
| 179 ) |
| 180 |
| 181 |
88 if __name__ == '__main__': | 182 if __name__ == '__main__': |
89 unittest.main() | 183 unittest.main() |
OLD | NEW |