1#!/usr/bin/env python 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 4# found in the LICENSE file. 5 6'''Unit tests for grit.format.resource_map''' 7 8import os 9import sys 10if __name__ == '__main__': 11 sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) 12 13import StringIO 14import unittest 15 16from grit import grd_reader 17from grit import util 18from grit.format import resource_map 19 20 21class FormatResourceMapUnittest(unittest.TestCase): 22 def testFormatResourceMap(self): 23 grd = grd_reader.Parse(StringIO.StringIO( 24 '''<?xml version="1.0" encoding="UTF-8"?> 25 <grit latest_public_release="2" source_lang_id="en" current_release="3" 26 base_dir="."> 27 <outputs> 28 <output type="rc_header" filename="the_rc_header.h" /> 29 <output type="resource_map_header" 30 filename="the_resource_map_header.h" /> 31 </outputs> 32 <release seq="3"> 33 <structures first_id="300"> 34 <structure type="menu" name="IDC_KLONKMENU" 35 file="grit\\testdata\\klonk.rc" encoding="utf-16" /> 36 </structures> 37 <includes first_id="10000"> 38 <include type="foo" file="abc" name="IDS_FIRSTPRESENT" /> 39 <if expr="False"> 40 <include type="foo" file="def" name="IDS_MISSING" /> 41 </if> 42 <if expr="lang != 'es'"> 43 <include type="foo" file="ghi" name="IDS_LANGUAGESPECIFIC" /> 44 </if> 45 <if expr="lang == 'es'"> 46 <include type="foo" file="jkl" name="IDS_LANGUAGESPECIFIC" /> 47 </if> 48 <include type="foo" file="mno" name="IDS_THIRDPRESENT" /> 49 </includes> 50 </release> 51 </grit>'''), util.PathFromRoot('.')) 52 grd.SetOutputLanguage('en') 53 grd.RunGatherers() 54 output = util.StripBlankLinesAndComments(''.join( 55 resource_map.GetFormatter('resource_map_header')(grd, 'en', '.'))) 56 self.assertEqual('''\ 57#include <stddef.h> 58#ifndef GRIT_RESOURCE_MAP_STRUCT_ 59#define GRIT_RESOURCE_MAP_STRUCT_ 60struct GritResourceMap { 61 const char* const name; 62 int value; 63}; 64#endif // GRIT_RESOURCE_MAP_STRUCT_ 65extern const GritResourceMap kTheRcHeader[]; 66extern const size_t kTheRcHeaderSize;''', output) 67 output = util.StripBlankLinesAndComments(''.join( 68 resource_map.GetFormatter('resource_map_source')(grd, 'en', '.'))) 69 self.assertEqual('''\ 70#include "the_resource_map_header.h" 71#include "base/basictypes.h" 72#include "the_rc_header.h" 73const GritResourceMap kTheRcHeader[] = { 74 {"IDC_KLONKMENU", IDC_KLONKMENU}, 75 {"IDS_FIRSTPRESENT", IDS_FIRSTPRESENT}, 76 {"IDS_MISSING", IDS_MISSING}, 77 {"IDS_LANGUAGESPECIFIC", IDS_LANGUAGESPECIFIC}, 78 {"IDS_THIRDPRESENT", IDS_THIRDPRESENT}, 79}; 80const size_t kTheRcHeaderSize = arraysize(kTheRcHeader);''', output) 81 output = util.StripBlankLinesAndComments(''.join( 82 resource_map.GetFormatter('resource_file_map_source')(grd, 'en', '.'))) 83 self.assertEqual('''\ 84#include "the_resource_map_header.h" 85#include "base/basictypes.h" 86#include "the_rc_header.h" 87const GritResourceMap kTheRcHeader[] = { 88 {"grit/testdata/klonk.rc", IDC_KLONKMENU}, 89 {"abc", IDS_FIRSTPRESENT}, 90 {"def", IDS_MISSING}, 91 {"ghi", IDS_LANGUAGESPECIFIC}, 92 {"jkl", IDS_LANGUAGESPECIFIC}, 93 {"mno", IDS_THIRDPRESENT}, 94}; 95const size_t kTheRcHeaderSize = arraysize(kTheRcHeader);''', output) 96 97 def testFormatResourceMapWithOutputAllEqualsFalseForStructures(self): 98 grd = grd_reader.Parse(StringIO.StringIO( 99 '''<?xml version="1.0" encoding="UTF-8"?> 100 <grit latest_public_release="2" source_lang_id="en" current_release="3" 101 base_dir="." output_all_resource_defines="false"> 102 <outputs> 103 <output type="rc_header" filename="the_rc_header.h" /> 104 <output type="resource_map_header" 105 filename="the_resource_map_header.h" /> 106 <output type="resource_map_source" 107 filename="the_resource_map_header.cc" /> 108 </outputs> 109 <release seq="3"> 110 <structures first_id="300"> 111 <structure type="chrome_scaled_image" name="IDR_KLONKMENU" 112 file="foo.png" /> 113 <if expr="False"> 114 <structure type="chrome_scaled_image" name="IDR_MISSING" 115 file="bar.png" /> 116 </if> 117 </structures> 118 </release> 119 </grit>'''), util.PathFromRoot('.')) 120 grd.SetOutputLanguage('en') 121 grd.RunGatherers() 122 output = util.StripBlankLinesAndComments(''.join( 123 resource_map.GetFormatter('resource_map_header')(grd, 'en', '.'))) 124 self.assertEqual('''\ 125#include <stddef.h> 126#ifndef GRIT_RESOURCE_MAP_STRUCT_ 127#define GRIT_RESOURCE_MAP_STRUCT_ 128struct GritResourceMap { 129 const char* const name; 130 int value; 131}; 132#endif // GRIT_RESOURCE_MAP_STRUCT_ 133extern const GritResourceMap kTheRcHeader[]; 134extern const size_t kTheRcHeaderSize;''', output) 135 output = util.StripBlankLinesAndComments(''.join( 136 resource_map.GetFormatter('resource_map_source')(grd, 'en', '.'))) 137 self.assertEqual('''\ 138#include "the_resource_map_header.h" 139#include "base/basictypes.h" 140#include "the_rc_header.h" 141const GritResourceMap kTheRcHeader[] = { 142 {"IDR_KLONKMENU", IDR_KLONKMENU}, 143}; 144const size_t kTheRcHeaderSize = arraysize(kTheRcHeader);''', output) 145 output = util.StripBlankLinesAndComments(''.join( 146 resource_map.GetFormatter('resource_map_source')(grd, 'en', '.'))) 147 self.assertEqual('''\ 148#include "the_resource_map_header.h" 149#include "base/basictypes.h" 150#include "the_rc_header.h" 151const GritResourceMap kTheRcHeader[] = { 152 {"IDR_KLONKMENU", IDR_KLONKMENU}, 153}; 154const size_t kTheRcHeaderSize = arraysize(kTheRcHeader);''', output) 155 156 def testFormatResourceMapWithOutputAllEqualsFalseForIncludes(self): 157 grd = grd_reader.Parse(StringIO.StringIO( 158 '''<?xml version="1.0" encoding="UTF-8"?> 159 <grit latest_public_release="2" source_lang_id="en" current_release="3" 160 base_dir="." output_all_resource_defines="false"> 161 <outputs> 162 <output type="rc_header" filename="the_rc_header.h" /> 163 <output type="resource_map_header" 164 filename="the_resource_map_header.h" /> 165 </outputs> 166 <release seq="3"> 167 <structures first_id="300"> 168 <structure type="menu" name="IDC_KLONKMENU" 169 file="grit\\testdata\\klonk.rc" encoding="utf-16" /> 170 </structures> 171 <includes first_id="10000"> 172 <include type="foo" file="abc" name="IDS_FIRSTPRESENT" /> 173 <if expr="False"> 174 <include type="foo" file="def" name="IDS_MISSING" /> 175 </if> 176 <include type="foo" file="mno" name="IDS_THIRDPRESENT" /> 177 </includes> 178 </release> 179 </grit>'''), util.PathFromRoot('.')) 180 grd.SetOutputLanguage('en') 181 grd.RunGatherers() 182 output = util.StripBlankLinesAndComments(''.join( 183 resource_map.GetFormatter('resource_map_header')(grd, 'en', '.'))) 184 self.assertEqual('''\ 185#include <stddef.h> 186#ifndef GRIT_RESOURCE_MAP_STRUCT_ 187#define GRIT_RESOURCE_MAP_STRUCT_ 188struct GritResourceMap { 189 const char* const name; 190 int value; 191}; 192#endif // GRIT_RESOURCE_MAP_STRUCT_ 193extern const GritResourceMap kTheRcHeader[]; 194extern const size_t kTheRcHeaderSize;''', output) 195 output = util.StripBlankLinesAndComments(''.join( 196 resource_map.GetFormatter('resource_map_source')(grd, 'en', '.'))) 197 self.assertEqual('''\ 198#include "the_resource_map_header.h" 199#include "base/basictypes.h" 200#include "the_rc_header.h" 201const GritResourceMap kTheRcHeader[] = { 202 {"IDC_KLONKMENU", IDC_KLONKMENU}, 203 {"IDS_FIRSTPRESENT", IDS_FIRSTPRESENT}, 204 {"IDS_THIRDPRESENT", IDS_THIRDPRESENT}, 205}; 206const size_t kTheRcHeaderSize = arraysize(kTheRcHeader);''', output) 207 output = util.StripBlankLinesAndComments(''.join( 208 resource_map.GetFormatter('resource_file_map_source')(grd, 'en', '.'))) 209 self.assertEqual('''\ 210#include "the_resource_map_header.h" 211#include "base/basictypes.h" 212#include "the_rc_header.h" 213const GritResourceMap kTheRcHeader[] = { 214 {"grit/testdata/klonk.rc", IDC_KLONKMENU}, 215 {"abc", IDS_FIRSTPRESENT}, 216 {"mno", IDS_THIRDPRESENT}, 217}; 218const size_t kTheRcHeaderSize = arraysize(kTheRcHeader);''', output) 219 220 def testFormatStringResourceMap(self): 221 grd = grd_reader.Parse(StringIO.StringIO( 222 '''<?xml version="1.0" encoding="UTF-8"?> 223 <grit latest_public_release="2" source_lang_id="en" current_release="3" 224 base_dir="."> 225 <outputs> 226 <output type="rc_header" filename="the_rc_header.h" /> 227 <output type="resource_map_header" filename="the_rc_map_header.h" /> 228 <output type="resource_map_source" filename="the_rc_map_source.cc" /> 229 </outputs> 230 <release seq="1" allow_pseudo="false"> 231 <messages fallback_to_english="true"> 232 <message name="IDS_PRODUCT_NAME" desc="The application name"> 233 Application 234 </message> 235 <if expr="True"> 236 <message name="IDS_DEFAULT_TAB_TITLE_TITLE_CASE" 237 desc="In Title Case: The default title in a tab."> 238 New Tab 239 </message> 240 </if> 241 <if expr="False"> 242 <message name="IDS_DEFAULT_TAB_TITLE" 243 desc="The default title in a tab."> 244 New tab 245 </message> 246 </if> 247 </messages> 248 </release> 249 </grit>'''), util.PathFromRoot('.')) 250 grd.SetOutputLanguage('en') 251 grd.RunGatherers() 252 output = util.StripBlankLinesAndComments(''.join( 253 resource_map.GetFormatter('resource_map_header')(grd, 'en', '.'))) 254 self.assertEqual('''\ 255#include <stddef.h> 256#ifndef GRIT_RESOURCE_MAP_STRUCT_ 257#define GRIT_RESOURCE_MAP_STRUCT_ 258struct GritResourceMap { 259 const char* const name; 260 int value; 261}; 262#endif // GRIT_RESOURCE_MAP_STRUCT_ 263extern const GritResourceMap kTheRcHeader[]; 264extern const size_t kTheRcHeaderSize;''', output) 265 output = util.StripBlankLinesAndComments(''.join( 266 resource_map.GetFormatter('resource_map_source')(grd, 'en', '.'))) 267 self.assertEqual('''\ 268#include "the_rc_map_header.h" 269#include "base/basictypes.h" 270#include "the_rc_header.h" 271const GritResourceMap kTheRcHeader[] = { 272 {"IDS_PRODUCT_NAME", IDS_PRODUCT_NAME}, 273 {"IDS_DEFAULT_TAB_TITLE_TITLE_CASE", IDS_DEFAULT_TAB_TITLE_TITLE_CASE}, 274}; 275const size_t kTheRcHeaderSize = arraysize(kTheRcHeader);''', output) 276 277 278if __name__ == '__main__': 279 unittest.main() 280