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.tclib''' 7 8 9 import sys 10 import os.path 11 if __name__ == '__main__': 12 sys.path.append(os.path.join(os.path.dirname(__file__), '..')) 13 14 import types 15 import unittest 16 17 from grit import tclib 18 19 from grit import exception 20 import grit.extern.tclib 21 22 23 class TclibUnittest(unittest.TestCase): 24 def testInit(self): 25 msg = tclib.Message(text=u'Hello Earthlings', 26 description='Greetings\n\t message') 27 self.failUnlessEqual(msg.GetPresentableContent(), 'Hello Earthlings') 28 self.failUnless(isinstance(msg.GetPresentableContent(), types.StringTypes)) 29 self.failUnlessEqual(msg.GetDescription(), 'Greetings message') 30 31 def testGetAttr(self): 32 msg = tclib.Message() 33 msg.AppendText(u'Hello') # Tests __getattr__ 34 self.failUnless(msg.GetPresentableContent() == 'Hello') 35 self.failUnless(isinstance(msg.GetPresentableContent(), types.StringTypes)) 36 37 def testAll(self): 38 text = u'Howdie USERNAME' 39 phs = [tclib.Placeholder(u'USERNAME', u'%s', 'Joi')] 40 msg = tclib.Message(text=text, placeholders=phs) 41 self.failUnless(msg.GetPresentableContent() == 'Howdie USERNAME') 42 43 trans = tclib.Translation(text=text, placeholders=phs) 44 self.failUnless(trans.GetPresentableContent() == 'Howdie USERNAME') 45 self.failUnless(isinstance(trans.GetPresentableContent(), types.StringTypes)) 46 47 def testUnicodeReturn(self): 48 text = u'\u00fe' 49 msg = tclib.Message(text=text) 50 self.failUnless(msg.GetPresentableContent() == text) 51 from_list = msg.GetContent()[0] 52 self.failUnless(from_list == text) 53 54 def testRegressionTranslationInherited(self): 55 '''Regression tests a bug that was caused by grit.tclib.Translation 56 inheriting from the translation console's Translation object 57 instead of only owning an instance of it. 58 ''' 59 msg = tclib.Message(text=u"BLA1\r\nFrom: BLA2 \u00fe BLA3", 60 placeholders=[ 61 tclib.Placeholder('BLA1', '%s', '%s'), 62 tclib.Placeholder('BLA2', '%s', '%s'), 63 tclib.Placeholder('BLA3', '%s', '%s')]) 64 transl = tclib.Translation(text=msg.GetPresentableContent(), 65 placeholders=msg.GetPlaceholders()) 66 content = transl.GetContent() 67 self.failUnless(isinstance(content[3], types.UnicodeType)) 68 69 def testFingerprint(self): 70 # This has Windows line endings. That is on purpose. 71 id = grit.extern.tclib.GenerateMessageId( 72 'Google Desktop for Enterprise\r\n' 73 'Copyright (C) 2006 Google Inc.\r\n' 74 'All Rights Reserved\r\n' 75 '\r\n' 76 '---------\r\n' 77 'Contents\r\n' 78 '---------\r\n' 79 'This distribution contains the following files:\r\n' 80 '\r\n' 81 'GoogleDesktopSetup.msi - Installation and setup program\r\n' 82 'GoogleDesktop.adm - Group Policy administrative template file\r\n' 83 'AdminGuide.pdf - Google Desktop for Enterprise administrative guide\r\n' 84 '\r\n' 85 '\r\n' 86 '--------------\r\n' 87 'Documentation\r\n' 88 '--------------\r\n' 89 'Full documentation and installation instructions are in the \r\n' 90 'administrative guide, and also online at \r\n' 91 'http://desktop.google.com/enterprise/adminguide.html.\r\n' 92 '\r\n' 93 '\r\n' 94 '------------------------\r\n' 95 'IBM Lotus Notes Plug-In\r\n' 96 '------------------------\r\n' 97 'The Lotus Notes plug-in is included in the release of Google \r\n' 98 'Desktop for Enterprise. The IBM Lotus Notes Plug-in for Google \r\n' 99 'Desktop indexes mail, calendar, task, contact and journal \r\n' 100 'documents from Notes. Discussion documents including those from \r\n' 101 'the discussion and team room templates can also be indexed by \r\n' 102 'selecting an option from the preferences. Once indexed, this data\r\n' 103 'will be returned in Google Desktop searches. The corresponding\r\n' 104 'document can be opened in Lotus Notes from the Google Desktop \r\n' 105 'results page.\r\n' 106 '\r\n' 107 'Install: The plug-in will install automatically during the Google \r\n' 108 'Desktop setup process if Lotus Notes is already installed. Lotus \r\n' 109 'Notes must not be running in order for the install to occur. \r\n' 110 '\r\n' 111 'Preferences: Preferences and selection of databases to index are\r\n' 112 'set in the \'Google Desktop for Notes\' dialog reached through the \r\n' 113 '\'Actions\' menu.\r\n' 114 '\r\n' 115 'Reindexing: Selecting \'Reindex all databases\' will index all the \r\n' 116 'documents in each database again.\r\n' 117 '\r\n' 118 '\r\n' 119 'Notes Plug-in Known Issues\r\n' 120 '---------------------------\r\n' 121 '\r\n' 122 'If the \'Google Desktop for Notes\' item is not available from the \r\n' 123 'Lotus Notes Actions menu, then installation was not successful. \r\n' 124 'Installation consists of writing one file, notesgdsplugin.dll, to \r\n' 125 'the Notes application directory and a setting to the notes.ini \r\n' 126 'configuration file. The most likely cause of an unsuccessful \r\n' 127 'installation is that the installer was not able to locate the \r\n' 128 'notes.ini file. Installation will complete if the user closes Notes\r\n' 129 'and manually adds the following setting to this file on a new line:\r\n' 130 'AddinMenus=notegdsplugin.dll\r\n' 131 '\r\n' 132 'If the notesgdsplugin.dll file is not in the application directory\r\n' 133 '(e.g., C:\Program Files\Lotus\Notes) after Google Desktop \r\n' 134 'installation, it is likely that Notes was not installed correctly. \r\n' 135 '\r\n' 136 'Only local databases can be indexed. If they can be determined, \r\n' 137 'the user\'s local mail file and address book will be included in the\r\n' 138 'list automatically. Mail archives and other databases must be \r\n' 139 'added with the \'Add\' button.\r\n' 140 '\r\n' 141 'Some users may experience performance issues during the initial \r\n' 142 'indexing of a database. The \'Perform the initial index of a \r\n' 143 'database only when I\'m idle\' option will limit the indexing process\r\n' 144 'to times when the user is not using the machine. If this does not \r\n' 145 'alleviate the problem or the user would like to continually index \r\n' 146 'but just do so more slowly or quickly, the GoogleWaitTime notes.ini\r\n' 147 'value can be set. Increasing the GoogleWaitTime value will slow \r\n' 148 'down the indexing process, and lowering the value will speed it up.\r\n' 149 'A value of zero causes the fastest possible indexing. Removing the\r\n' 150 'ini parameter altogether returns it to the default (20).\r\n' 151 '\r\n' 152 'Crashes have been known to occur with certain types of history \r\n' 153 'bookmarks. If the Notes client seems to crash randomly, try \r\n' 154 'disabling the \'Index note history\' option. If it crashes before,\r\n' 155 'you can get to the preferences, add the following line to your \r\n' 156 'notes.ini file:\r\n' 157 'GDSNoIndexHistory=1\r\n') 158 self.failUnless(id == '8961534701379422820') 159 160 def testPlaceholderNameChecking(self): 161 try: 162 ph = tclib.Placeholder('BINGO BONGO', 'bla', 'bla') 163 raise Exception("We shouldn't get here") 164 except exception.InvalidPlaceholderName: 165 pass # Expect exception to be thrown because presentation contained space 166 167 def testTagsWithCommonSubstring(self): 168 word = 'ABCDEFGHIJ' 169 text = ' '.join([word[:i] for i in range(1, 11)]) 170 phs = [tclib.Placeholder(word[:i], str(i), str(i)) for i in range(1, 11)] 171 try: 172 msg = tclib.Message(text=text, placeholders=phs) 173 self.failUnless(msg.GetRealContent() == '1 2 3 4 5 6 7 8 9 10') 174 except: 175 self.fail('tclib.Message() should handle placeholders that are ' 176 'substrings of each other') 177 178 if __name__ == '__main__': 179 unittest.main() 180