• 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 xmb' tool.'''
7
8import os
9import sys
10if __name__ == '__main__':
11  sys.path.append(os.path.join(os.path.dirname(__file__), '../..'))
12
13import unittest
14import StringIO
15
16from grit import grd_reader
17from grit import util
18from grit.tool import xmb
19
20
21class XmbUnittest(unittest.TestCase):
22  def setUp(self):
23    self.res_tree = grd_reader.Parse(
24        StringIO.StringIO(u'''<?xml version="1.0" encoding="UTF-8"?>
25      <grit latest_public_release="2" source_lang_id="en-US" current_release="3" base_dir=".">
26        <release seq="3">
27          <includes>
28            <include type="gif" name="ID_LOGO" file="images/logo.gif" />
29          </includes>
30          <messages>
31            <message name="GOOD" desc="sub" sub_variable="true">
32              excellent
33            </message>
34            <message name="IDS_GREETING" desc="Printed to greet the currently logged in user">
35              Hello <ph name="USERNAME">%s<ex>Joi</ex></ph>, are you doing [GOOD] today?
36            </message>
37            <message name="IDS_BONGOBINGO">
38              Yibbee
39            </message>
40          </messages>
41          <structures>
42            <structure type="dialog" name="IDD_SPACYBOX" encoding="utf-16" file="grit/testdata/klonk.rc" />
43          </structures>
44        </release>
45      </grit>'''), '.')
46    self.xmb_file = StringIO.StringIO()
47
48  def testNormalOutput(self):
49    xmb.OutputXmb().Process(self.res_tree, self.xmb_file)
50    output = self.xmb_file.getvalue()
51    self.failUnless(output.count('Joi') and output.count('Yibbee'))
52
53  def testLimitList(self):
54    limit_file = StringIO.StringIO(
55      'IDS_BONGOBINGO\nIDS_DOES_NOT_EXIST\nIDS_ALSO_DOES_NOT_EXIST')
56    xmb.OutputXmb().Process(self.res_tree, self.xmb_file, limit_file, False)
57    output = self.xmb_file.getvalue()
58    self.failUnless(output.count('Yibbee'))
59    self.failUnless(not output.count('Joi'))
60
61  def testLimitGrd(self):
62    limit_file = StringIO.StringIO('''<?xml version="1.0" encoding="UTF-8"?>
63      <grit latest_public_release="2" source_lang_id="en-US" current_release="3" base_dir=".">
64        <release seq="3">
65          <messages>
66            <message name="IDS_GREETING" desc="Printed to greet the currently logged in user">
67              Hello <ph name="USERNAME">%s<ex>Joi</ex></ph>, how are you doing today?
68            </message>
69          </messages>
70        </release>
71      </grit>''')
72    tool = xmb.OutputXmb()
73    class DummyOpts(object):
74      extra_verbose = False
75    tool.o = DummyOpts()
76    tool.Process(self.res_tree, self.xmb_file, limit_file, True, dir='.')
77    output = self.xmb_file.getvalue()
78    self.failUnless(output.count('Joi'))
79    self.failUnless(not output.count('Yibbee'))
80
81  def testSubstitution(self):
82    self.res_tree.SetOutputLanguage('en')
83    os.chdir(util.PathFromRoot('.'))  # so it can find klonk.rc
84    self.res_tree.RunGatherers()
85    xmb.OutputXmb().Process(self.res_tree, self.xmb_file)
86    output = self.xmb_file.getvalue()
87    self.failUnless(output.count(
88        '<ph name="GOOD_1"><ex>excellent</ex>[GOOD]</ph>'))
89
90  def testLeadingTrailingWhitespace(self):
91    # Regression test for problems outputting messages with leading or
92    # trailing whitespace (these come in via structures only, as
93    # message nodes already strip and store whitespace).
94    self.res_tree.SetOutputLanguage('en')
95    os.chdir(util.PathFromRoot('.'))  # so it can find klonk.rc
96    self.res_tree.RunGatherers()
97    xmb.OutputXmb().Process(self.res_tree, self.xmb_file)
98    output = self.xmb_file.getvalue()
99    self.failUnless(output.count('OK ? </msg>'))
100
101
102if __name__ == '__main__':
103  unittest.main()
104