1# Copyright 2019 The Chromium Authors. All rights reserved.
2# Use of this source code is governed by a BSD-style license that can be
3# found in the LICENSE file.
4
5import unittest
6
7from android_chrome_version import GenerateVersionCodes
8
9
10class _VersionTest(unittest.TestCase):
11  """Unittests for the android_chrome_version module.
12  """
13
14  EXAMPLE_VERSION_VALUES = {
15      'MAJOR': '74',
16      'MINOR': '0',
17      'BUILD': '3720',
18      'PATCH': '0',
19  }
20
21  def testGenerateVersionCodesAndroidChrome(self):
22    """Assert it gives correct values for standard/example inputs"""
23    output = GenerateVersionCodes(
24        self.EXAMPLE_VERSION_VALUES, arch='arm', is_next_build=False)
25
26    chrome_version_code = output['CHROME_VERSION_CODE']
27
28    self.assertEqual(chrome_version_code, '372000000')
29
30  def testGenerateVersionCodesAndroidChromeModern(self):
31    """Assert it gives correct values for standard/example inputs"""
32    output = GenerateVersionCodes(
33        self.EXAMPLE_VERSION_VALUES, arch='arm', is_next_build=False)
34
35    chrome_modern_version_code = output['CHROME_MODERN_VERSION_CODE']
36
37    self.assertEqual(chrome_modern_version_code, '372000010')
38
39  def testGenerateVersionCodesAndroidMonochrome(self):
40    """Assert it gives correct values for standard/example inputs"""
41    output = GenerateVersionCodes(
42        self.EXAMPLE_VERSION_VALUES, arch='arm', is_next_build=False)
43
44    monochrome_version_code = output['MONOCHROME_VERSION_CODE']
45
46    self.assertEqual(monochrome_version_code, '372000020')
47
48  def testGenerateVersionCodesAndroidTrichrome(self):
49    """Assert it gives correct values for standard/example inputs"""
50    output = GenerateVersionCodes(
51        self.EXAMPLE_VERSION_VALUES, arch='arm', is_next_build=False)
52
53    trichrome_version_code = output['TRICHROME_VERSION_CODE']
54
55    self.assertEqual(trichrome_version_code, '372000030')
56
57  def testGenerateVersionCodesAndroidWebviewStable(self):
58    """Assert it gives correct values for standard/example inputs"""
59    output = GenerateVersionCodes(
60        self.EXAMPLE_VERSION_VALUES, arch='arm', is_next_build=False)
61
62    webview_stable_version_code = output['WEBVIEW_STABLE_VERSION_CODE']
63
64    self.assertEqual(webview_stable_version_code, '372000000')
65
66  def testGenerateVersionCodesAndroidWebviewBeta(self):
67    """Assert it gives correct values for standard/example inputs"""
68    output = GenerateVersionCodes(
69        self.EXAMPLE_VERSION_VALUES, arch='arm', is_next_build=False)
70
71    webview_beta_version_code = output['WEBVIEW_BETA_VERSION_CODE']
72
73    self.assertEqual(webview_beta_version_code, '372000010')
74
75  def testGenerateVersionCodesAndroidWebviewDev(self):
76    """Assert it gives correct values for standard/example inputs"""
77    output = GenerateVersionCodes(
78        self.EXAMPLE_VERSION_VALUES, arch='arm', is_next_build=False)
79
80    webview_dev_version_code = output['WEBVIEW_DEV_VERSION_CODE']
81
82    self.assertEqual(webview_dev_version_code, '372000020')
83
84  def testGenerateVersionCodesAndroidNextBuild(self):
85    """Assert it handles "next" builds correctly"""
86    output = GenerateVersionCodes(
87        self.EXAMPLE_VERSION_VALUES, arch='arm', is_next_build=True)
88
89    # Get just a sample of values
90    chrome_version_code = output['CHROME_VERSION_CODE']
91    monochrome_version_code = output['MONOCHROME_VERSION_CODE']
92    webview_stable_version_code = output['WEBVIEW_STABLE_VERSION_CODE']
93    webview_beta_version_code = output['WEBVIEW_BETA_VERSION_CODE']
94
95    self.assertEqual(chrome_version_code, '372000050')
96    self.assertEqual(monochrome_version_code, '372000070')
97    self.assertEqual(webview_stable_version_code, '372000050')
98    self.assertEqual(webview_beta_version_code, '372000060')
99
100  def testGenerateVersionCodesAndroidArchArm(self):
101    """Assert it handles different architectures correctly.
102
103    Version codes for different builds need to be distinct and maintain a
104    certain ordering.
105    See docs in android_chrome_version._ABIS_TO_BIT_MASK for
106    reasoning.
107    """
108    output = GenerateVersionCodes(
109        self.EXAMPLE_VERSION_VALUES, arch='arm', is_next_build=False)
110    arch_chrome_version_code = output['CHROME_VERSION_CODE']
111
112    self.assertEqual(arch_chrome_version_code, '372000000')
113
114  def testGenerateVersionCodesAndroidArchX86(self):
115    """Assert it handles different architectures correctly.
116
117    Version codes for different builds need to be distinct and maintain a
118    certain ordering.
119    See docstring on android_chrome_version._ABIS_TO_BIT_MASK for
120    reasoning.
121    """
122    output = GenerateVersionCodes(
123        self.EXAMPLE_VERSION_VALUES, arch='x86', is_next_build=False)
124    arch_chrome_version_code = output['CHROME_VERSION_CODE']
125
126    self.assertEqual(arch_chrome_version_code, '372000001')
127
128  def testGenerateVersionCodesAndroidArchMips(self):
129    """Assert it handles different architectures correctly.
130
131    Version codes for different builds need to be distinct and maintain a
132    certain ordering.
133    See docstring on android_chrome_version._ABIS_TO_BIT_MASK for
134    reasoning.
135    """
136    output = GenerateVersionCodes(
137        self.EXAMPLE_VERSION_VALUES, arch='mipsel', is_next_build=False)
138    arch_chrome_version_code = output['CHROME_VERSION_CODE']
139
140    self.assertEqual(arch_chrome_version_code, '372000002')
141
142  def testGenerateVersionCodesAndroidArchArm64(self):
143    """Assert it handles different architectures correctly.
144
145    Version codes for different builds need to be distinct and maintain a
146    certain ordering.
147    See docstring on android_chrome_version._ABIS_TO_BIT_MASK for
148    reasoning.
149    """
150    output = GenerateVersionCodes(
151        self.EXAMPLE_VERSION_VALUES, arch='arm64', is_next_build=False)
152    arch_chrome_version_code = output['CHROME_VERSION_CODE']
153
154    self.assertEqual(arch_chrome_version_code, '372000005')
155
156  def testGenerateVersionCodesAndroidArchArm64Variants(self):
157    """Assert it handles 64-bit-specific additional version codes correctly.
158
159    Some additional version codes are generated for 64-bit architectures.
160    See docstring on android_chrome_version.ARCH64_APK_VARIANTS for more info.
161    """
162    output = GenerateVersionCodes(
163        self.EXAMPLE_VERSION_VALUES, arch='arm64', is_next_build=False)
164    arch_monochrome_version_code = output['MONOCHROME_VERSION_CODE']
165    arch_monochrome_32_version_code = output['MONOCHROME_32_VERSION_CODE']
166    arch_monochrome_32_64_version_code = output['MONOCHROME_32_64_VERSION_CODE']
167    arch_monochrome_64_32_version_code = output['MONOCHROME_64_32_VERSION_CODE']
168    arch_monochrome_64_version_code = output['MONOCHROME_64_VERSION_CODE']
169    arch_trichrome_version_code = output['TRICHROME_VERSION_CODE']
170    arch_trichrome_32_version_code = output['TRICHROME_32_VERSION_CODE']
171    arch_trichrome_32_64_version_code = output['TRICHROME_32_64_VERSION_CODE']
172    arch_trichrome_64_32_version_code = output['TRICHROME_64_32_VERSION_CODE']
173    arch_trichrome_64_version_code = output['TRICHROME_64_VERSION_CODE']
174
175    self.assertEqual(arch_monochrome_32_version_code, '372000020')
176    self.assertEqual(arch_monochrome_32_64_version_code, '372000023')
177    self.assertEqual(arch_monochrome_version_code, '372000023')
178    self.assertEqual(arch_monochrome_64_32_version_code, '372000024')
179    self.assertEqual(arch_monochrome_64_version_code, '372000025')
180    self.assertEqual(arch_trichrome_32_version_code, '372000030')
181    self.assertEqual(arch_trichrome_32_64_version_code, '372000033')
182    self.assertEqual(arch_trichrome_version_code, '372000033')
183    self.assertEqual(arch_trichrome_64_32_version_code, '372000034')
184    self.assertEqual(arch_trichrome_64_version_code, '372000035')
185
186  def testGenerateVersionCodesAndroidArchX64(self):
187    """Assert it handles different architectures correctly.
188
189    Version codes for different builds need to be distinct and maintain a
190    certain ordering.
191    See docstring on android_chrome_version._ABIS_TO_BIT_MASK for
192    reasoning.
193    """
194    output = GenerateVersionCodes(
195        self.EXAMPLE_VERSION_VALUES, arch='x64', is_next_build=False)
196    arch_chrome_version_code = output['CHROME_VERSION_CODE']
197
198    self.assertEqual(arch_chrome_version_code, '372000008')
199
200  def testGenerateVersionCodesAndroidArchX64Variants(self):
201    """Assert it handles 64-bit-specific additional version codes correctly.
202
203    Some additional version codes are generated for 64-bit architectures.
204    See docstring on android_chrome_version.ARCH64_APK_VARIANTS for more info.
205    """
206    output = GenerateVersionCodes(
207        self.EXAMPLE_VERSION_VALUES, arch='x64', is_next_build=False)
208    arch_monochrome_32_version_code = output['MONOCHROME_32_VERSION_CODE']
209    arch_monochrome_32_64_version_code = output['MONOCHROME_32_64_VERSION_CODE']
210    arch_monochrome_version_code = output['MONOCHROME_VERSION_CODE']
211    arch_monochrome_64_32_version_code = output['MONOCHROME_64_32_VERSION_CODE']
212    arch_monochrome_64_version_code = output['MONOCHROME_64_VERSION_CODE']
213    arch_trichrome_32_version_code = output['TRICHROME_32_VERSION_CODE']
214    arch_trichrome_32_64_version_code = output['TRICHROME_32_64_VERSION_CODE']
215    arch_trichrome_version_code = output['TRICHROME_VERSION_CODE']
216    arch_trichrome_64_32_version_code = output['TRICHROME_64_32_VERSION_CODE']
217    arch_trichrome_64_version_code = output['TRICHROME_64_VERSION_CODE']
218
219    self.assertEqual(arch_monochrome_32_version_code, '372000021')
220    self.assertEqual(arch_monochrome_32_64_version_code, '372000026')
221    self.assertEqual(arch_monochrome_version_code, '372000026')
222    self.assertEqual(arch_monochrome_64_32_version_code, '372000027')
223    self.assertEqual(arch_monochrome_64_version_code, '372000028')
224    self.assertEqual(arch_trichrome_32_version_code, '372000031')
225    self.assertEqual(arch_trichrome_32_64_version_code, '372000036')
226    self.assertEqual(arch_trichrome_version_code, '372000036')
227    self.assertEqual(arch_trichrome_64_32_version_code, '372000037')
228    self.assertEqual(arch_trichrome_64_version_code, '372000038')
229
230  def testGenerateVersionCodesAndroidArchOrderArm(self):
231    """Assert it handles different architectures correctly.
232
233    Version codes for different builds need to be distinct and maintain a
234    certain ordering.
235    See docstring on android_chrome_version._ABIS_TO_BIT_MASK for
236    reasoning.
237
238    Test arm-related values.
239    """
240    arm_output = GenerateVersionCodes(
241        self.EXAMPLE_VERSION_VALUES, arch='arm', is_next_build=False)
242    arm64_output = GenerateVersionCodes(
243        self.EXAMPLE_VERSION_VALUES, arch='arm64', is_next_build=False)
244
245    arm_chrome_version_code = arm_output['CHROME_VERSION_CODE']
246    arm64_chrome_version_code = arm64_output['CHROME_VERSION_CODE']
247
248    self.assertLess(arm_chrome_version_code, arm64_chrome_version_code)
249
250  def testGenerateVersionCodesAndroidArchOrderX86(self):
251    """Assert it handles different architectures correctly.
252
253    Version codes for different builds need to be distinct and maintain a
254    certain ordering.
255    See docstring on android_chrome_version._ABIS_TO_BIT_MASK for
256    reasoning.
257
258    Test x86-related values.
259    """
260    x86_output = GenerateVersionCodes(
261        self.EXAMPLE_VERSION_VALUES, arch='x86', is_next_build=False)
262    x64_output = GenerateVersionCodes(
263        self.EXAMPLE_VERSION_VALUES, arch='x64', is_next_build=False)
264
265    x86_chrome_version_code = x86_output['CHROME_VERSION_CODE']
266    x64_chrome_version_code = x64_output['CHROME_VERSION_CODE']
267
268    self.assertLess(x86_chrome_version_code, x64_chrome_version_code)
269
270  def testGenerateVersionCodesAndroidWebviewChannelOrderBeta(self):
271    """Assert webview beta channel is higher than stable.
272
273    The channel-specific version codes for standalone webview needs to follow
274    the order stable < beta < dev.
275
276    This allows that if a user opts into beta track, they will always have the
277    beta apk, including any finch experiments targeted at beta users, even when
278    beta and stable channels are otherwise on the same version.
279    """
280    output = GenerateVersionCodes(
281        self.EXAMPLE_VERSION_VALUES, arch='arm', is_next_build=False)
282
283    webview_stable_version_code = output['WEBVIEW_STABLE_VERSION_CODE']
284    webview_beta_version_code = output['WEBVIEW_BETA_VERSION_CODE']
285
286    self.assertGreater(webview_beta_version_code, webview_stable_version_code)
287
288  def testGenerateVersionCodesAndroidWebviewChannelOrderDev(self):
289    """Assert webview dev channel is higher than beta.
290
291    The channel-specific version codes for standalone webview needs to follow
292    the order stable < beta < dev.
293
294    This allows that if a user opts into dev track, they will always have the
295    dev apk, including any finch experiments targeted at dev users, even when
296    dev and beta channels are otherwise on the same version.
297    """
298    output = GenerateVersionCodes(
299        self.EXAMPLE_VERSION_VALUES, arch='arm', is_next_build=False)
300
301    webview_beta_version_code = output['WEBVIEW_BETA_VERSION_CODE']
302    webview_dev_version_code = output['WEBVIEW_DEV_VERSION_CODE']
303
304    self.assertGreater(webview_dev_version_code, webview_beta_version_code)
305
306
307if __name__ == '__main__':
308  unittest.main()
309