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 the admin template gatherer.''' 7 8 import os 9 import sys 10 if __name__ == '__main__': 11 sys.path.append(os.path.join(os.path.dirname(__file__), '../..')) 12 13 import StringIO 14 import tempfile 15 import unittest 16 17 from grit.gather import admin_template 18 from grit import util 19 from grit import grd_reader 20 from grit import grit_runner 21 from grit.tool import build 22 23 24 class AdmGathererUnittest(unittest.TestCase): 25 def testParsingAndTranslating(self): 26 pseudofile = StringIO.StringIO( 27 'bingo bongo\n' 28 'ding dong\n' 29 '[strings] \n' 30 'whatcha="bingo bongo"\n' 31 'gotcha = "bingolabongola "the wise" fingulafongula" \n') 32 gatherer = admin_template.AdmGatherer(pseudofile) 33 gatherer.Parse() 34 self.failUnless(len(gatherer.GetCliques()) == 2) 35 self.failUnless(gatherer.GetCliques()[1].GetMessage().GetRealContent() == 36 'bingolabongola "the wise" fingulafongula') 37 38 translation = gatherer.Translate('en') 39 self.failUnless(translation == gatherer.GetText().strip()) 40 41 def testErrorHandling(self): 42 pseudofile = StringIO.StringIO( 43 'bingo bongo\n' 44 'ding dong\n' 45 'whatcha="bingo bongo"\n' 46 'gotcha = "bingolabongola "the wise" fingulafongula" \n') 47 gatherer = admin_template.AdmGatherer(pseudofile) 48 self.assertRaises(admin_template.MalformedAdminTemplateException, 49 gatherer.Parse) 50 51 _TRANSLATABLES_FROM_FILE = ( 52 'Google', 'Google Desktop', 'Preferences', 53 'Controls Google Desktop preferences', 54 'Indexing and Capture Control', 55 'Controls what files, web pages, and other content will be indexed by Google Desktop.', 56 'Prevent indexing of email', 57 # there are lots more but we don't check any further 58 ) 59 60 def VerifyCliquesFromAdmFile(self, cliques): 61 self.failUnless(len(cliques) > 20) 62 for clique, expected in zip(cliques, self._TRANSLATABLES_FROM_FILE): 63 text = clique.GetMessage().GetRealContent() 64 self.failUnless(text == expected) 65 66 def testFromFile(self): 67 fname = util.PathFromRoot('grit/testdata/GoogleDesktop.adm') 68 gatherer = admin_template.AdmGatherer(fname) 69 gatherer.Parse() 70 cliques = gatherer.GetCliques() 71 self.VerifyCliquesFromAdmFile(cliques) 72 73 def MakeGrd(self): 74 grd = grd_reader.Parse(StringIO.StringIO('''<?xml version="1.0" encoding="UTF-8"?> 75 <grit latest_public_release="2" source_lang_id="en-US" current_release="3"> 76 <release seq="3"> 77 <structures> 78 <structure type="admin_template" name="IDAT_GOOGLE_DESKTOP_SEARCH" 79 file="GoogleDesktop.adm" exclude_from_rc="true" /> 80 <structure type="txt" name="BINGOBONGO" 81 file="README.txt" exclude_from_rc="true" /> 82 </structures> 83 </release> 84 <outputs> 85 <output filename="de_res.rc" type="rc_all" lang="de" /> 86 </outputs> 87 </grit>'''), util.PathFromRoot('grit/testdata')) 88 grd.SetOutputLanguage('en') 89 grd.RunGatherers() 90 return grd 91 92 def testInGrd(self): 93 grd = self.MakeGrd() 94 cliques = grd.children[0].children[0].children[0].GetCliques() 95 self.VerifyCliquesFromAdmFile(cliques) 96 97 def testFileIsOutput(self): 98 grd = self.MakeGrd() 99 dirname = tempfile.mkdtemp() 100 try: 101 tool = build.RcBuilder() 102 tool.o = grit_runner.Options() 103 tool.output_directory = dirname 104 tool.res = grd 105 tool.Process() 106 107 self.failUnless(os.path.isfile( 108 os.path.join(dirname, 'de_GoogleDesktop.adm'))) 109 self.failUnless(os.path.isfile( 110 os.path.join(dirname, 'de_README.txt'))) 111 finally: 112 for f in os.listdir(dirname): 113 os.unlink(os.path.join(dirname, f)) 114 os.rmdir(dirname) 115 116 if __name__ == '__main__': 117 unittest.main() 118