blob: 85296fab5e2365b1bb311014bcd809d0824601ba [file] [log] [blame]
Copybara854996b2021-09-07 19:36:02 +00001# Copyright 2016 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style
3# license that can be found in the LICENSE file or at
4# https://developers.google.com/open-source/licenses/bsd
5
6"""Unit tests for template_helpers module."""
7
8from __future__ import division
9from __future__ import print_function
10from __future__ import absolute_import
11
12import unittest
13
14from framework import pbproxy_test_pb2
15from framework import template_helpers
16
17
18class HelpersUnitTest(unittest.TestCase):
19
20 def testDictionaryProxy(self):
21
22 # basic in 'n out test
23 item = template_helpers.EZTItem(label='foo', group_name='bar')
24
25 self.assertEqual('foo', item.label)
26 self.assertEqual('bar', item.group_name)
27
28 # be sure the __str__ returns the fields
29 self.assertEqual(
30 "EZTItem({'group_name': 'bar', 'label': 'foo'})", str(item))
31
32 def testPBProxy(self):
33 """Checks that PBProxy wraps protobuf objects as expected."""
34 # check that protobuf fields are accessible in ".attribute" form
35 pbe = pbproxy_test_pb2.PBProxyExample()
36 pbe.nickname = 'foo'
37 pbe.invited = False
38 pbep = template_helpers.PBProxy(pbe)
39 self.assertEqual(pbep.nickname, 'foo')
40 # _bool suffix converts protobuf field 'bar' to None (EZT boolean false)
41 self.assertEqual(pbep.invited_bool, None)
42
43 # check that a new field can be added to the PBProxy
44 pbep.baz = 'bif'
45 self.assertEqual(pbep.baz, 'bif')
46
47 # check that a PBProxy-local field can hide a protobuf field
48 pbep.nickname = 'local foo'
49 self.assertEqual(pbep.nickname, 'local foo')
50
51 # check that a nested protobuf is recursively wrapped with a PBProxy
52 pbn = pbproxy_test_pb2.PBProxyNested()
53 pbn.nested = pbproxy_test_pb2.PBProxyExample()
54 pbn.nested.nickname = 'bar'
55 pbn.nested.invited = True
56 pbnp = template_helpers.PBProxy(pbn)
57 self.assertEqual(pbnp.nested.nickname, 'bar')
58 # _bool suffix converts protobuf field 'bar' to 'yes' (EZT boolean true)
59 self.assertEqual(pbnp.nested.invited_bool, 'yes')
60
61 # check that 'repeated' lists of items produce a list of strings
62 pbn.multiple_strings.append('1')
63 pbn.multiple_strings.append('2')
64 self.assertEqual(pbnp.multiple_strings, ['1', '2'])
65
66 # check that 'repeated' messages produce lists of PBProxy instances
67 pbe1 = pbproxy_test_pb2.PBProxyExample()
68 pbn.multiple_pbes.append(pbe1)
69 pbe1.nickname = '1'
70 pbe1.invited = True
71 pbe2 = pbproxy_test_pb2.PBProxyExample()
72 pbn.multiple_pbes.append(pbe2)
73 pbe2.nickname = '2'
74 pbe2.invited = False
75 self.assertEqual(pbnp.multiple_pbes[0].nickname, '1')
76 self.assertEqual(pbnp.multiple_pbes[0].invited_bool, 'yes')
77 self.assertEqual(pbnp.multiple_pbes[1].nickname, '2')
78 self.assertEqual(pbnp.multiple_pbes[1].invited_bool, None)
79
80 def testFitTextMethods(self):
81 """Tests both FitUnsafeText with an eye on i18n."""
82 # pylint: disable=anomalous-unicode-escape-in-string
83 test_data = (
84 u'This is a short string.',
85
86 u'This is a much longer string. '
87 u'This is a much longer string. '
88 u'This is a much longer string. '
89 u'This is a much longer string. '
90 u'This is a much longer string. '
91 u'This is a much longer string. '
92 u'This is a much longer string. '
93 u'This is a much longer string. '
94 u'This is a much longer string. '
95 u'This is a much longer string. ',
96
97 # This is a short escaped i18n string
98 '\xd5\xa1\xd5\xba\xd5\xa1\xd5\xaf\xd5\xab'.decode('utf-8'),
99
100 # This is a longer i18n string
101 '\xd5\xa1\xd5\xba\xd5\xa1\xd5\xaf\xd5\xab '
102 '\xe6\x88\x91\xe8\x83\xbd\xe5\x90\x9e '
103 '\xd5\xa1\xd5\xba\xd5\xa1\xd5\xaf\xd5\xab '
104 '\xe6\x88\x91\xe8\x83\xbd\xe5\x90\x9e '
105 '\xd5\xa1\xd5\xba\xd5\xa1\xd5\xaf\xd5\xab '
106 '\xe6\x88\x91\xe8\x83\xbd\xe5\x90\x9e '
107 '\xd5\xa1\xd5\xba\xd5\xa1\xd5\xaf\xd5\xab '
108 '\xe6\x88\x91\xe8\x83\xbd\xe5\x90\x9e '.decode('utf-8'),
109
110 # This is a longer i18n string that was causing trouble.
111 '\u041d\u0430 \u0431\u0435\u0440\u0435\u0433\u0443'
112 ' \u043f\u0443\u0441\u0442\u044b\u043d\u043d\u044b\u0445'
113 ' \u0432\u043e\u043b\u043d \u0421\u0442\u043e\u044f\u043b'
114 ' \u043e\u043d, \u0434\u0443\u043c'
115 ' \u0432\u0435\u043b\u0438\u043a\u0438\u0445'
116 ' \u043f\u043e\u043b\u043d, \u0418'
117 ' \u0432\u0434\u0430\u043b\u044c'
118 ' \u0433\u043b\u044f\u0434\u0435\u043b.'
119 ' \u041f\u0440\u0435\u0434 \u043d\u0438\u043c'
120 ' \u0448\u0438\u0440\u043e\u043a\u043e'
121 ' \u0420\u0435\u043a\u0430'
122 ' \u043d\u0435\u0441\u043b\u0430\u0441\u044f;'
123 ' \u0431\u0435\u0434\u043d\u044b\u0439'
124 ' \u0447\u0451\u043b\u043d \u041f\u043e'
125 ' \u043d\u0435\u0439'
126 ' \u0441\u0442\u0440\u0435\u043c\u0438\u043b\u0441\u044f'
127 ' \u043e\u0434\u0438\u043d\u043e\u043a\u043e.'
128 ' \u041f\u043e \u043c\u0448\u0438\u0441\u0442\u044b\u043c,'
129 ' \u0442\u043e\u043f\u043a\u0438\u043c'
130 ' \u0431\u0435\u0440\u0435\u0433\u0430\u043c'
131 ' \u0427\u0435\u0440\u043d\u0435\u043b\u0438'
132 ' \u0438\u0437\u0431\u044b \u0437\u0434\u0435\u0441\u044c'
133 ' \u0438 \u0442\u0430\u043c, \u041f\u0440\u0438\u044e\u0442'
134 ' \u0443\u0431\u043e\u0433\u043e\u0433\u043e'
135 ' \u0447\u0443\u0445\u043e\u043d\u0446\u0430;'
136 ' \u0418 \u043b\u0435\u0441,'
137 ' \u043d\u0435\u0432\u0435\u0434\u043e\u043c\u044b\u0439'
138 ' \u043b\u0443\u0447\u0430\u043c \u0412'
139 ' \u0442\u0443\u043c\u0430\u043d\u0435'
140 ' \u0441\u043f\u0440\u044f\u0442\u0430\u043d\u043d\u043e'
141 '\u0433\u043e \u0441\u043e\u043b\u043d\u0446\u0430,'
142 ' \u041a\u0440\u0443\u0433\u043e\u043c'
143 ' \u0448\u0443\u043c\u0435\u043b.'.decode('utf-8'))
144
145 for unicode_s in test_data:
146 # Get the length in characters, not bytes.
147 length = len(unicode_s)
148
149 # Test the FitUnsafeText method at the length boundary.
150 fitted_unsafe_text = template_helpers.FitUnsafeText(unicode_s, length)
151 self.assertEqual(fitted_unsafe_text, unicode_s)
152
153 # Set some values that test FitString well.
154 available_space = length // 2
155 max_trailing = length // 4
156 # Break the string at various places - symmetric range around 0
157 for i in range(1-max_trailing, max_trailing):
158 # Test the FitUnsafeText method.
159 fitted_unsafe_text = template_helpers.FitUnsafeText(
160 unicode_s, available_space - i)
161 self.assertEqual(fitted_unsafe_text[:available_space - i],
162 unicode_s[:available_space - i])
163
164 # Test a string that is already unicode
165 u_string = u'This is already unicode'
166 fitted_unsafe_text = template_helpers.FitUnsafeText(u_string, 100)
167 self.assertEqual(u_string, fitted_unsafe_text)
168
169 # Test a string that is already unicode, and has non-ascii in it.
170 u_string = u'This is already unicode este\\u0301tico'
171 fitted_unsafe_text = template_helpers.FitUnsafeText(u_string, 100)
172 self.assertEqual(u_string, fitted_unsafe_text)
173
174 def testEZTError(self):
175 errors = template_helpers.EZTError()
176 self.assertFalse(errors.AnyErrors())
177
178 errors.error_a = 'A'
179 self.assertTrue(errors.AnyErrors())
180 self.assertEqual('A', errors.error_a)
181
182 errors.SetError('error_b', 'B')
183 self.assertTrue(errors.AnyErrors())
184 self.assertEqual('A', errors.error_a)
185 self.assertEqual('B', errors.error_b)
186
187 def testBytesKbOrMb(self):
188 self.assertEqual('1023 bytes', template_helpers.BytesKbOrMb(1023))
189 self.assertEqual('1.0 KB', template_helpers.BytesKbOrMb(1024))
190 self.assertEqual('1023 KB', template_helpers.BytesKbOrMb(1024 * 1023))
191 self.assertEqual('1.0 MB', template_helpers.BytesKbOrMb(1024 * 1024))
192 self.assertEqual('98.0 MB', template_helpers.BytesKbOrMb(98 * 1024 * 1024))
193 self.assertEqual('99 MB', template_helpers.BytesKbOrMb(99 * 1024 * 1024))
194
195
196class TextRunTest(unittest.TestCase):
197
198 def testLink(self):
199 run = template_helpers.TextRun(
200 'content', tag='a', href='http://example.com')
201 expected = '<a href="http://example.com">content</a>'
202 self.assertEqual(expected, run.FormatForHTMLEmail())
203
204 run = template_helpers.TextRun(
205 'con<tent>', tag='a', href='http://exa"mple.com')
206 expected = '<a href="http://exa&quot;mple.com">con&lt;tent&gt;</a>'
207 self.assertEqual(expected, run.FormatForHTMLEmail())
208
209 def testText(self):
210 run = template_helpers.TextRun('content')
211 expected = 'content'
212 self.assertEqual(expected, run.FormatForHTMLEmail())
213
214 run = template_helpers.TextRun('con<tent>')
215 expected = 'con&lt;tent&gt;'
216 self.assertEqual(expected, run.FormatForHTMLEmail())