• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 testFormatStringResourceMap(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=".">
102        <outputs>
103          <output type="rc_header" filename="the_rc_header.h" />
104          <output type="resource_map_header" filename="the_rc_map_header.h" />
105          <output type="resource_map_source" filename="the_rc_map_source.cc" />
106        </outputs>
107        <release seq="1" allow_pseudo="false">
108          <messages fallback_to_english="true">
109            <message name="IDS_PRODUCT_NAME" desc="The application name">
110              Application
111            </message>
112            <if expr="1">
113              <message name="IDS_DEFAULT_TAB_TITLE_TITLE_CASE"
114                  desc="In Title Case: The default title in a tab.">
115                New Tab
116              </message>
117            </if>
118            <if expr="0">
119              <message name="IDS_DEFAULT_TAB_TITLE"
120                  desc="The default title in a tab.">
121                New tab
122              </message>
123            </if>
124          </messages>
125        </release>
126      </grit>'''), util.PathFromRoot('.'))
127    grd.SetOutputLanguage('en')
128    grd.RunGatherers()
129    output = util.StripBlankLinesAndComments(''.join(
130        resource_map.GetFormatter('resource_map_header')(grd, 'en', '.')))
131    self.assertEqual('''\
132#include <stddef.h>
133#ifndef GRIT_RESOURCE_MAP_STRUCT_
134#define GRIT_RESOURCE_MAP_STRUCT_
135struct GritResourceMap {
136  const char* const name;
137  int value;
138};
139#endif // GRIT_RESOURCE_MAP_STRUCT_
140extern const GritResourceMap kTheRcHeader[];
141extern const size_t kTheRcHeaderSize;''', output)
142    output = util.StripBlankLinesAndComments(''.join(
143        resource_map.GetFormatter('resource_map_source')(grd, 'en', '.')))
144    self.assertEqual('''\
145#include "the_rc_map_header.h"
146#include "base/basictypes.h"
147#include "the_rc_header.h"
148const GritResourceMap kTheRcHeader[] = {
149  {"IDS_PRODUCT_NAME", IDS_PRODUCT_NAME},
150  {"IDS_DEFAULT_TAB_TITLE_TITLE_CASE", IDS_DEFAULT_TAB_TITLE_TITLE_CASE},
151};
152const size_t kTheRcHeaderSize = arraysize(kTheRcHeader);''', output)
153
154
155if __name__ == '__main__':
156  unittest.main()
157