Upstream version 5.34.98.0
[platform/framework/web/crosswalk.git] / src / xwalk / app / tools / android / make_apk_test.py
1 #!/usr/bin/env python
2
3 # Copyright (c) 2013, 2014 Intel Corporation. All rights reserved.
4 # Use of this source code is governed by a BSD-style license that can be
5 # found in the LICENSE file.
6
7 import optparse
8 import os
9 import shutil
10 import subprocess
11 import sys
12 import unittest
13 import warnings
14
15 def Clean(name, app_version):
16   if os.path.exists(name):
17     shutil.rmtree(name)
18   if options.mode == 'shared':
19     if os.path.isfile(name + '_' + app_version + '.apk'):
20       os.remove(name + '_' + app_version + '.apk')
21   else:
22     if os.path.isfile(name + '_' + app_version + '_x86.apk'):
23       os.remove(name + '_' + app_version + '_x86.apk')
24     if os.path.isfile(name + '_' + app_version + '_arm.apk'):
25       os.remove(name + '_' + app_version + '_arm.apk')
26
27
28 def RunCommand(command):
29   """Runs the command list, return the output."""
30   proc = subprocess.Popen(command, stdout=subprocess.PIPE,
31                           stderr=subprocess.STDOUT, shell=False)
32   return proc.communicate()[0]
33
34
35 class TestMakeApk(unittest.TestCase):
36   @classmethod
37   def setUpClass(cls):
38     cls._original_dir = os.getcwd()
39     if options.tool_path:
40       target_dir = os.path.expanduser(options.tool_path)
41     elif options.build_dir and options.target:
42       target_dir = os.path.join(options.build_dir,
43                                 options.target,
44                                 'xwalk_app_template')
45     if os.path.exists(target_dir):
46       # Prepare the test data.
47       test_src_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)),
48                                   'test_data')
49       test_des_dir = os.path.join(target_dir, 'test_data')
50       if not os.path.exists(test_des_dir):
51         shutil.copytree(test_src_dir, test_des_dir)
52       os.chdir(target_dir)
53     else:
54       unittest.SkipTest('xwalk_app_template folder doesn\'t exist. '
55                         'Skipping all tests in make_apk_test.py')
56     cls._mode = ''
57     if options.mode == 'shared':
58       cls._mode = '--mode=shared'
59     elif options.mode == 'embedded':
60       cls._mode = '--mode=embedded'
61     cls.fakeNativeLibrary()
62
63   @classmethod
64   def tearDownClass(cls):
65     # Clean the test data.
66     if options.tool_path:
67       test_data_dir = os.path.join(os.path.expanduser(options.tool_path),
68                                    'test_data')
69     elif options.build_dir and options.target:
70       test_data_dir = os.path.join(options.build_dir,
71                                    options.target,
72                                    'xwalk_app_template',
73                                    'test_data')
74     if os.path.exists(test_data_dir):
75       shutil.rmtree(test_data_dir)
76     cls.restoreNativeLibrary()
77     os.chdir(cls._original_dir)
78
79   @staticmethod
80   def fakeNativeLibrary():
81     # To reduce the time consumption of make_apk test for embedded mode,
82     # replace the original native library with an empty library.
83     # Because it doesn't affect the result of test.
84     if options.mode == 'embedded':
85       native_library_dir = 'native_libs'
86       native_library_temp_dir = 'temp'
87       shutil.copytree(native_library_dir, native_library_temp_dir)
88       for root, _, files in os.walk(native_library_dir):
89         if 'libxwalkcore.so' in files:
90           native_library_path = os.path.join(root, 'libxwalkcore.so')
91           # Remove the original library
92           os.remove(native_library_path)
93           # Create an empty library file
94           open(native_library_path, 'a').close()
95
96   @staticmethod
97   def restoreNativeLibrary():
98     # Restore the original native library for embedded mode.
99     if options.mode == 'embedded':
100       native_library_dir = 'native_libs'
101       native_library_temp_dir = 'temp'
102       if os.path.exists(native_library_dir):
103         shutil.rmtree(native_library_dir)
104       shutil.move(native_library_temp_dir, native_library_dir)
105
106   @staticmethod
107   def archs():
108     x86_native_lib_path = os.path.join('native_libs', 'x86', 'libs',
109                                        'x86', 'libxwalkcore.so')
110     arm_native_lib_path = os.path.join('native_libs', 'armeabi-v7a', 'libs',
111                                        'armeabi-v7a', 'libxwalkcore.so')
112     arch_list = []
113     if os.path.isfile(x86_native_lib_path):
114       arch_list.append('x86')
115     if os.path.isfile(arm_native_lib_path):
116       arch_list.append('arm')
117     return arch_list
118
119   def checkApks(self, apk_name, app_version):
120     # Check whether some files are contained in the given APK.
121     if self._mode.find('shared') != -1:
122       apk_path = '%s_%s.apk' % (apk_name, app_version)
123       self.checkApk(apk_path, '')
124     elif self._mode.find('embedded') != -1:
125       x86_apk_path = '%s_%s_x86.apk' % (apk_name, app_version)
126       if os.path.exists(x86_apk_path):
127         self.checkApk(x86_apk_path, 'x86')
128       arm_apk_path = '%s_%s_arm.apk' % (apk_name, app_version)
129       if os.path.exists(arm_apk_path):
130         self.checkApk(arm_apk_path, 'arm')
131
132   def checkApk(self, apk_path, arch):
133     # Check whether some files are contained in the given apk
134     # for specified arch.
135     cmd = ['jar', 'tvf', apk_path]
136     out = RunCommand(cmd)
137     common_files = ['AndroidManifest.xml', 'classes.dex']
138     for res_file in common_files:
139       self.assertTrue(out.find(res_file) != -1)
140     if self._mode.find('embedded') != -1:
141       embedded_related_files = ['xwalk.pak',
142                                 'device_capabilities_api.js',
143                                 'launch_screen_api.js',
144                                 'presentation_api.js',
145                                 'screen_orientation_api.js']
146       for res_file in embedded_related_files:
147         self.assertTrue(out.find(res_file) != -1)
148     if arch == 'x86':
149       self.assertTrue(out.find('x86/libxwalkcore.so') != -1)
150     elif arch == 'arm':
151       self.assertTrue(out.find('armeabi-v7a/libxwalkcore.so') != -1)
152
153   def testName(self):
154     cmd = ['python', 'make_apk.py', '--app-version=1.0.0',
155            '--package=org.xwalk.example', self._mode]
156     out = RunCommand(cmd)
157     self.assertTrue(out.find('The APK name is required!') != -1)
158     Clean('Example', '1.0.0')
159     cmd = ['python', 'make_apk.py', '--name="Test Example"',
160            '--app-version=1.0.0',
161            '--package=org.xwalk.example', self._mode]
162     out = RunCommand(cmd)
163     self.assertTrue(out.find('The APK name is required!') == -1)
164     Clean('Test Example', '1.0.0')
165     # The following invalid chars verification is too heavy for embedded mode,
166     # and the result of verification should be the same between shared mode
167     # and embedded mode. So only do the verification in the shared mode.
168     if self._mode.find('shared') != -1:
169       invalid_chars = '\/:.*?"<>|-'
170       for c in invalid_chars:
171         invalid_name = '--name=Example' + c
172         cmd = ['python', 'make_apk.py', invalid_name,
173                '--app-version=1.0.0', '--package=org.xwalk.example',
174                '--app-url=http://www.intel.com', self._mode]
175         out = RunCommand(cmd)
176         self.assertTrue(out.find('Illegal character') != -1)
177         Clean('Example_', '1.0.0')
178
179   def testToolVersion(self):
180     cmd = ['python', 'make_apk.py', '--version']
181     out = RunCommand(cmd)
182     self.assertTrue(out.find('Crosswalk app packaging tool version') != -1)
183
184   def testAppDescriptionAndVersion(self):
185     cmd = ['python', 'make_apk.py', '--name=Example',
186            '--package=org.xwalk.example', '--app-version=1.0.0',
187            '--description=a sample application',
188            '--app-url=http://www.intel.com', self._mode]
189     RunCommand(cmd)
190     manifest = 'Example/AndroidManifest.xml'
191     with open(manifest, 'r') as content_file:
192       content = content_file.read()
193     self.assertTrue(os.path.exists(manifest))
194     self.assertTrue(content.find('description') != -1)
195     self.assertTrue(content.find('versionName') != -1)
196     self.checkApks('Example', '1.0.0')
197     Clean('Example', '1.0.0')
198
199   def testAppVersionCode(self):
200     cmd = ['python', 'make_apk.py', '--name=Example',
201            '--package=org.xwalk.example', '--app-version=1.0.0',
202            '--description=a sample application',
203            '--app-versionCode=3',
204            '--app-url=http://www.intel.com', self._mode]
205     RunCommand(cmd)
206     manifest = 'Example/AndroidManifest.xml'
207     with open(manifest, 'r') as content_file:
208       content = content_file.read()
209     self.assertTrue(os.path.exists(manifest))
210     self.assertTrue(content.find('versionCode="3"') != -1)
211     self.checkApks('Example', '1.0.0')
212     Clean('Example', '1.0.0')
213
214   def testAppVersionCodeBase(self):
215     # Arch option only works for embedded mode,
216     # so only test it for embedded mode.
217     if self._mode.find('embedded') == -1:
218       return
219     if 'x86' in self.archs():
220       arch = '--arch=x86'
221       versionCode = 'versionCode="60000003"'
222     else:
223       arch = '--arch=arm'
224       versionCode = 'versionCode="20000003"'
225     cmd = ['python', 'make_apk.py', '--name=Example',
226            '--package=org.xwalk.example', '--app-version=1.0.0',
227            '--description=a sample application',
228            '--app-versionCodeBase=3',
229            arch,
230            '--app-url=http://www.intel.com', self._mode]
231     RunCommand(cmd)
232     manifest = 'Example/AndroidManifest.xml'
233     with open(manifest, 'r') as content_file:
234       content = content_file.read()
235     self.assertTrue(os.path.exists(manifest))
236     self.assertTrue(content.find(versionCode) != -1)
237     self.checkApks('Example', '1.0.0')
238     Clean('Example', '1.0.0')
239
240   def testAppBigVersionCodeBase(self):
241     # Arch option only works for embedded mode,
242     # so only test it for embedded mode.
243     if self._mode.find('embedded') == -1:
244       return
245     if 'x86' in self.archs():
246       arch = '--arch=x86'
247     else:
248       arch = '--arch=arm'
249     cmd = ['python', 'make_apk.py', '--name=Example',
250            '--package=org.xwalk.example', '--app-version=1.0.0',
251            '--description=a sample application',
252            '--app-versionCodeBase=30000000',
253            arch,
254            '--app-url=http://www.intel.com', self._mode]
255     RunCommand(cmd)
256     manifest = 'Example/AndroidManifest.xml'
257     self.assertFalse(os.path.exists(manifest))
258     Clean('Example', '1.0.0')
259
260   def testPermissions(self):
261     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
262            '--package=org.xwalk.example', '--permissions=geolocation',
263            '--app-url=http://www.intel.com', self._mode]
264     RunCommand(cmd)
265     manifest = 'Example/AndroidManifest.xml'
266     with open(manifest, 'r') as content_file:
267       content = content_file.read()
268     self.assertTrue(os.path.exists(manifest))
269     self.assertTrue(content.find('ACCESS_FINE_LOCATION') != -1)
270     self.checkApks('Example', '1.0.0')
271     Clean('Example', '1.0.0')
272
273   def testPackage(self):
274     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
275            self._mode]
276     out = RunCommand(cmd)
277     self.assertTrue(out.find('The package name is required!') != -1)
278     Clean('Example', '1.0.0')
279     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
280            '--package=org.xwalk.example', self._mode]
281     out = RunCommand(cmd)
282     self.assertTrue(out.find('The package name is required!') == -1)
283     Clean('Example', '1.0.0')
284
285   def testEntry(self):
286     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
287            '--package=org.xwalk.example', '--app-url=http://www.intel.com',
288            self._mode]
289     out = RunCommand(cmd)
290     self.assertTrue(out.find('The entry is required.') == -1)
291     self.checkApks('Example', '1.0.0')
292     Clean('Example', '1.0.0')
293
294     test_entry_root = 'test_data/entry'
295     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
296            '--package=org.xwalk.example', '--app-root=%s' % test_entry_root,
297            '--app-local-path=index.html', self._mode]
298     out = RunCommand(cmd)
299     self.assertTrue(out.find('The entry is required.') == -1)
300     self.checkApks('Example', '1.0.0')
301     Clean('Example', '1.0.0')
302
303   def testEntryWithErrors(self):
304     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
305            '--package=org.xwalk.example', self._mode]
306     out = RunCommand(cmd)
307     self.assertTrue(out.find('The entry is required.') != -1)
308     self.assertFalse(os.path.exists('Example.apk'))
309     Clean('Example', '1.0.0')
310
311     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
312            '--package=org.xwalk.example', '--app-url=http://www.intel.com',
313            '--app-root=.', self._mode]
314     out = RunCommand(cmd)
315     self.assertTrue(out.find('The entry is required.') != -1)
316     self.assertFalse(os.path.exists('Example.apk'))
317     Clean('Example', '1.0.0')
318
319     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
320            '--package=org.xwalk.example', '--app-root=./', self._mode]
321     out = RunCommand(cmd)
322     self.assertTrue(out.find('The entry is required.') != -1)
323     self.assertFalse(os.path.exists('Example.apk'))
324     Clean('Example', '1.0.0')
325
326     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
327            '--package=org.xwalk.example', '--app-local-path=index.html',
328            self._mode]
329     out = RunCommand(cmd)
330     self.assertTrue(out.find('The entry is required.') != -1)
331     self.assertFalse(os.path.exists('Example.apk'))
332     Clean('Example', '1.0.0')
333
334   def testIcon(self):
335     icon_path = './app_src/res/drawable-xhdpi/crosswalk.png'
336     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
337            '--package=org.xwalk.example', '--app-url=http://www.intel.com',
338            '--icon=%s' % icon_path, self._mode]
339     RunCommand(cmd)
340     manifest = 'Example/AndroidManifest.xml'
341     with open(manifest, 'r') as content_file:
342       content = content_file.read()
343     self.assertTrue(content.find('crosswalk') != -1)
344     self.assertTrue(os.path.exists('Example/res/drawable'))
345     self.checkApks('Example', '1.0.0')
346     Clean('Example', '1.0.0')
347
348   def testFullscreen(self):
349     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
350            '--package=org.xwalk.example', '--app-url=http://www.intel.com',
351            '-f', self._mode]
352     RunCommand(cmd)
353     theme = 'Example/res/values/theme.xml'
354     with open(theme, 'r') as content_file:
355       content = content_file.read()
356     self.assertTrue(os.path.exists(theme))
357     self.assertTrue(
358         content.find(
359             '<item name="android:windowFullscreen">true</item>') != -1)
360     self.checkApks('Example', '1.0.0')
361     Clean('Example', '1.0.0')
362
363   def testEnableRemoteDebugging(self):
364     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
365            '--package=org.xwalk.example', '--app-url=http://www.intel.com',
366            '--enable-remote-debugging', self._mode]
367     RunCommand(cmd)
368     activity = 'Example/src/org/xwalk/example/ExampleActivity.java'
369     with open(activity, 'r') as content_file:
370       content = content_file.read()
371     self.assertTrue(os.path.exists(activity))
372     self.assertTrue(content.find('setRemoteDebugging') != -1)
373     self.checkApks('Example', '1.0.0')
374     Clean('Example', '1.0.0')
375     manifest_path = os.path.join('test_data', 'manifest', 'manifest.json')
376     cmd = ['python', 'make_apk.py', '--enable-remote-debugging',
377            '--manifest=%s' % manifest_path, self._mode]
378     RunCommand(cmd)
379     activity = 'Example/src/org/xwalk/example/ExampleActivity.java'
380     with open(activity, 'r') as content_file:
381       content = content_file.read()
382     self.assertTrue(os.path.exists(activity))
383     self.assertTrue(content.find('setRemoteDebugging') != -1)
384     self.checkApks('Example', '1.0.0')
385     Clean('Example', '1.0.0')
386
387   def testKeystore(self):
388     keystore_path = os.path.join('test_data', 'keystore', 'xwalk-test.keystore')
389     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
390            '--package=org.xwalk.example', '--app-url=http://www.intel.com',
391            '--keystore-path=%s' % keystore_path, '--keystore-alias=xwalk-test',
392            '--keystore-passcode=xwalk-test', self._mode]
393     RunCommand(cmd)
394     self.assertTrue(os.path.exists('Example'))
395     apk_list = ['Example.apk', 'Example_x86.apk', 'Example_arm.apk']
396     for apk in apk_list:
397       if os.path.isfile(apk):
398         cmd = ['jarsigner', '-verify', '-keystore',
399                keystore_path, '-verbose', apk]
400         out = RunCommand(cmd)
401         self.assertTrue(out.find('smk') != -1)
402     self.checkApks('Example', '1.0.0')
403     Clean('Example', '1.0.0')
404
405   def testManifest(self):
406     manifest_path = os.path.join('test_data', 'manifest', 'manifest.json')
407     cmd = ['python', 'make_apk.py', '--manifest=%s' % manifest_path, self._mode]
408     RunCommand(cmd)
409     manifest = 'Example/AndroidManifest.xml'
410     with open(manifest, 'r') as content_file:
411       content = content_file.read()
412     self.assertTrue(os.path.exists(manifest))
413     self.assertTrue(content.find('android.permission.READ_CONTACTS') != -1)
414     self.assertTrue(content.find('android.permission.WRITE_CONTACTS') != -1)
415     self.assertTrue(
416         content.find('android.permission.ACCESS_FINE_LOCATION') != -1)
417     self.assertTrue(content.find('android.permission.READ_SMS') != -1)
418     self.assertTrue(content.find('android.permission.RECEIVE_SMS') != -1)
419     self.assertTrue(content.find('android.permission.SEND_SMS') != -1)
420     self.assertTrue(content.find('android.permission.WRITE_SMS') != -1)
421     theme = 'Example/res/values/theme.xml'
422     with open(theme, 'r') as content_file:
423       content = content_file.read()
424     self.assertTrue(os.path.exists(theme))
425     self.assertTrue(
426         content.find(
427             '<item name="android:windowFullscreen">true</item>') != -1)
428     self.assertTrue(os.path.exists('Example'))
429     self.checkApks('Example', '1.0.0')
430     Clean('Example', '1.0.0')
431
432   def testManifestWithSpecificValue(self):
433     manifest_path = os.path.join('test_data', 'manifest',
434                                  'manifest_app_launch_local_path.json')
435     cmd = ['python', 'make_apk.py', '--manifest=%s' % manifest_path, self._mode]
436     out = RunCommand(cmd)
437     self.assertTrue(out.find('no app launch path') == -1)
438     self.checkApks('Example', '1.0.0')
439     Clean('Example', '1.0.0')
440
441   def testManifestWithError(self):
442     manifest_path = os.path.join('test_data', 'manifest',
443                                  'manifest_no_app_launch_path.json')
444     cmd = ['python', 'make_apk.py', '--manifest=%s' % manifest_path,
445            '--verbose', self._mode]
446     out = RunCommand(cmd)
447     self.assertTrue(out.find('no app launch path') != -1)
448     manifest_path = os.path.join('test_data', 'manifest',
449                                  'manifest_no_name.json')
450     cmd = ['python', 'make_apk.py', '--manifest=%s' % manifest_path,
451            '--verbose', self._mode]
452     out = RunCommand(cmd)
453     self.assertTrue(out.find('no \'name\' field') != -1)
454     manifest_path = os.path.join('test_data', 'manifest',
455                                  'manifest_no_version.json')
456     cmd = ['python', 'make_apk.py', '--manifest=%s' % manifest_path,
457            '--verbose', self._mode]
458     out = RunCommand(cmd)
459     self.assertTrue(out.find('no \'version\' field') != -1)
460     manifest_path = os.path.join('test_data', 'manifest',
461                                  'manifest_permissions_format_error.json')
462     cmd = ['python', 'make_apk.py', '--manifest=%s' % manifest_path,
463            '--verbose', self._mode]
464     out = RunCommand(cmd)
465     self.assertTrue(out.find('\'Permissions\' field error') != -1)
466     manifest_path = os.path.join('test_data', 'manifest',
467                                  'manifest_permissions_field_error.json')
468     cmd = ['python', 'make_apk.py', '--manifest=%s' % manifest_path,
469            '--verbose', self._mode]
470     out = RunCommand(cmd)
471     self.assertTrue(out.find('\'Permissions\' field error') != -1)
472     manifest_path = os.path.join('test_data', 'manifest',
473                                  'manifest_not_supported_permission.json')
474     cmd = ['python', 'make_apk.py', '--manifest=%s' % manifest_path,
475            '--verbose', self._mode]
476     out = RunCommand(cmd)
477     self.assertTrue(
478         out.find('\'Telephony\' related API is not supported') != -1)
479
480   def testExtensionsWithOneExtension(self):
481     # Test with an existed extension.
482     extension_path = 'test_data/extensions/myextension'
483     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
484            '--package=org.xwalk.example', '--app-url=http://www.intel.com',
485            '--extensions=%s' % extension_path, self._mode]
486     RunCommand(cmd)
487     self.assertTrue(os.path.exists('Example'))
488     extensions_config_json = 'Example/assets/extensions-config.json'
489     self.assertTrue(os.path.exists(extensions_config_json))
490     with open(extensions_config_json, 'r') as content_file:
491       content = content_file.read()
492     self.assertTrue(content.find('xwalk-extensions/myextension/myextension.js'))
493     self.assertTrue(content.find('com.example.extension.MyExtension'))
494     extension_js = 'Example/assets/xwalk-extensions/myextension/myextension.js'
495     self.assertTrue(os.path.exists(extension_js))
496     extension_jar = 'Example/xwalk-extensions/myextension/myextension.jar'
497     self.assertTrue(os.path.exists(extension_jar))
498     self.checkApks('Example', '1.0.0')
499     Clean('Example', '1.0.0')
500
501   def testExtensionsWithNonExtension(self):
502     # Test with a non-existed extension.
503     extension_path = 'test_data/extensions/myextension'
504     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
505            '--package=org.xwalk.example', '--app-url=http://www.intel.com',
506            '--extensions=%s1' % extension_path, self._mode, '--verbose']
507     out = RunCommand(cmd)
508     error_msg = 'Error: can\'t find the extension directory'
509     self.assertTrue(out.find(error_msg) != -1)
510     self.assertTrue(out.find('Exiting with error code: 9') != -1)
511
512   def testExtensionWithPermissions(self):
513     test_entry_root = 'test_data/entry'
514     # Add redundant separators for test.
515     extension_path = 'test_data//extensions/contactextension/'
516     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
517            '--package=org.xwalk.example', '--app-root=%s' % test_entry_root,
518            '--app-local-path=contactextension.html',
519            '--extensions=%s' % extension_path, self._mode]
520     RunCommand(cmd)
521     self.assertTrue(os.path.exists('Example'))
522     manifest = 'Example/AndroidManifest.xml'
523     with open(manifest, 'r') as content_file:
524       content = content_file.read()
525     self.assertTrue(os.path.exists(manifest))
526     self.assertTrue(content.find('android.permission.WRITE_CONTACTS') != -1)
527     self.assertTrue(content.find('android.permission.READ_CONTACTS') != -1)
528     self.checkApks('Example', '1.0.0')
529     Clean('Example', '1.0.0')
530
531   def testXPK(self):
532     xpk_file = os.path.join('test_data', 'xpk', 'example.xpk')
533     cmd = ['python', 'make_apk.py', '--xpk=%s' % xpk_file, self._mode]
534     RunCommand(cmd)
535     self.assertTrue(os.path.exists('Example'))
536     self.checkApks('Example', '1.0.0')
537     Clean('Example', '1.0.0')
538
539   def testXPKWithError(self):
540     xpk_file = os.path.join('test_data', 'xpk', 'error.xpk')
541     cmd = ['python', 'make_apk.py', '--xpk=%s' % xpk_file, self._mode]
542     out = RunCommand(cmd)
543     error_msg = 'XPK doesn\'t contain manifest file'
544     self.assertTrue(out.find(error_msg) != -1)
545     self.assertFalse(os.path.exists('Example'))
546
547   def testOrientation(self):
548     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
549            '--package=org.xwalk.example', '--app-url=http://www.intel.com',
550            '--orientation=landscape', self._mode]
551     RunCommand(cmd)
552     manifest = 'Example/AndroidManifest.xml'
553     with open(manifest, 'r') as content_file:
554       content = content_file.read()
555     self.assertTrue(os.path.exists(manifest))
556     self.assertTrue(content.find('landscape') != -1)
557     self.assertTrue(os.path.exists('Example'))
558     self.checkApks('Example', '1.0.0')
559     Clean('Example', '1.0.0')
560
561   def testArch(self):
562     # Arch option only works for embedded mode,
563     # so only test it for embedded mode.
564     if self._mode.find('embedded') != -1:
565       cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
566              '--package=org.xwalk.example', '--app-url=http://www.intel.com',
567              '--arch=x86', self._mode]
568       RunCommand(cmd)
569       if 'x86' in self.archs():
570         self.assertTrue(os.path.isfile('Example_1.0.0_x86.apk'))
571         self.checkApk('Example_1.0.0_x86.apk', 'x86')
572       else:
573         self.assertFalse(os.path.isfile('Example_1.0.0_x86.apk'))
574       self.assertFalse(os.path.isfile('Example_1.0.0_arm.apk'))
575       Clean('Example', '1.0.0')
576       cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
577              '--package=org.xwalk.example', '--app-url=http://www.intel.com',
578              '--arch=arm', self._mode]
579       RunCommand(cmd)
580       if 'arm' in self.archs():
581         self.assertTrue(os.path.isfile('Example_1.0.0_arm.apk'))
582         self.checkApk('Example_1.0.0_arm.apk', 'arm')
583       else:
584         self.assertFalse(os.path.isfile('Example_1.0.0._arm.apk'))
585       self.assertFalse(os.path.isfile('Example_1.0.0_x86.apk'))
586       Clean('Example', '1.0.0')
587
588
589   def testVerbose(self):
590     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
591            '--package=org.xwalk.example', '--app-url=http://www.intel.com',
592            '--verbose', self._mode]
593     result = RunCommand(cmd)
594     self.assertTrue(result.find('aapt') != -1)
595     self.assertTrue(result.find('crunch') != -1)
596     self.assertTrue(result.find('apkbuilder') != -1)
597     self.assertTrue(os.path.exists('Example'))
598     self.checkApks('Example', '1.0.0')
599     Clean('Example', '1.0.0')
600
601
602   def testEmptyMode(self):
603     # Test all of supported options with empty 'mode' option.
604     icon_path = './app_src/res/drawable-xhdpi/crosswalk.png'
605     extension_path = 'test_data/extensions/myextension'
606     cmd = ['python', 'make_apk.py',
607            '--app-version=1.0.0',
608            '--app-url=http://www.intel.com',
609            '--arch=x86',
610            '--description=a sample application',
611            '--enable-remote-debugging',
612            '--extensions=%s' % extension_path,
613            '--fullscreen',
614            '--icon=%s' % icon_path,
615            '--name=Example',
616            '--orientation=landscape',
617            '--package=org.xwalk.example',
618            '--permissions=geolocation']
619     RunCommand(cmd)
620     activity = 'Example/src/org/xwalk/example/ExampleActivity.java'
621     with open(activity, 'r') as content_file:
622       content = content_file.read()
623     self.assertTrue(os.path.exists(activity))
624     # Test remote debugging option.
625     self.assertTrue(content.find('setRemoteDebugging') != -1)
626
627     manifest = 'Example/AndroidManifest.xml'
628     with open(manifest, 'r') as content_file:
629       content = content_file.read()
630     self.assertTrue(os.path.exists(manifest))
631     # Test permission option.
632     self.assertTrue(content.find('ACCESS_FINE_LOCATION') != -1)
633     # Test description option.
634     self.assertTrue(content.find('description') != -1)
635     # Test app version option.
636     self.assertTrue(content.find('versionName') != -1)
637     # Test orientation option.
638     self.assertTrue(content.find('landscape') != -1)
639     # Test icon option.
640     self.assertTrue(os.path.exists('Example/res/drawable'))
641     # Test fullscreen option
642     theme = 'Example/res/values/theme.xml'
643     with open(theme, 'r') as content_file:
644       content = content_file.read()
645     self.assertTrue(os.path.exists(theme))
646     self.assertTrue(
647         content.find(
648             '<item name="android:windowFullscreen">true</item>') != -1)
649     # Test extensions option.
650     extensions_config_json = 'Example/assets/extensions-config.json'
651     self.assertTrue(os.path.exists(extensions_config_json))
652     with open(extensions_config_json, 'r') as content_file:
653       content = content_file.read()
654       js_file_name = 'xwalk-extensions/myextension/myextension.js'
655       self.assertTrue(content.find(js_file_name))
656       self.assertTrue(content.find('com.example.extension.MyExtension'))
657     extension_js = 'Example/assets/xwalk-extensions/myextension/myextension.js'
658     self.assertTrue(os.path.exists(extension_js))
659     extension_jar = 'Example/xwalk-extensions/myextension/myextension.jar'
660     self.assertTrue(os.path.exists(extension_jar))
661     # Test arch option.
662     if 'x86' in self.archs():
663       self.assertTrue(os.path.isfile('Example_1.0.0_x86.apk'))
664       self.checkApk('Example_1.0.0_x86.apk', 'x86')
665     else:
666       self.assertFalse(os.path.isfile('Example_1.0.0_x86.apk'))
667     self.assertFalse(os.path.isfile('Example_1.0.0_arm.apk'))
668     Clean('Example', '1.0.0')
669
670
671 def SuiteWithModeOption():
672   # Gather all the tests for the specified mode option.
673   test_suite = unittest.TestSuite()
674   test_suite.addTest(TestMakeApk('testAppBigVersionCodeBase'))
675   test_suite.addTest(TestMakeApk('testAppVersionCode'))
676   test_suite.addTest(TestMakeApk('testAppVersionCodeBase'))
677   test_suite.addTest(TestMakeApk('testAppDescriptionAndVersion'))
678   test_suite.addTest(TestMakeApk('testArch'))
679   test_suite.addTest(TestMakeApk('testEnableRemoteDebugging'))
680   test_suite.addTest(TestMakeApk('testEntry'))
681   test_suite.addTest(TestMakeApk('testEntryWithErrors'))
682   test_suite.addTest(TestMakeApk('testExtensionsWithOneExtension'))
683   test_suite.addTest(TestMakeApk('testExtensionsWithNonExtension'))
684   test_suite.addTest(TestMakeApk('testExtensionWithPermissions'))
685   test_suite.addTest(TestMakeApk('testFullscreen'))
686   test_suite.addTest(TestMakeApk('testIcon'))
687   test_suite.addTest(TestMakeApk('testKeystore'))
688   test_suite.addTest(TestMakeApk('testManifest'))
689   test_suite.addTest(TestMakeApk('testManifestWithError'))
690   test_suite.addTest(TestMakeApk('testName'))
691   test_suite.addTest(TestMakeApk('testOrientation'))
692   test_suite.addTest(TestMakeApk('testPackage'))
693   test_suite.addTest(TestMakeApk('testPermissions'))
694   test_suite.addTest(TestMakeApk('testXPK'))
695   test_suite.addTest(TestMakeApk('testXPKWithError'))
696   return test_suite
697
698
699 def SuiteWithEmptyModeOption():
700   # Gather all the tests for empty mode option.
701   test_suite = unittest.TestSuite()
702   test_suite.addTest(TestMakeApk('testEmptyMode'))
703   test_suite.addTest(TestMakeApk('testToolVersion'))
704   test_suite.addTest(TestMakeApk('testVerbose'))
705   return test_suite
706
707
708 def TestSuiteRun(test_runner, suite):
709   results = test_runner.run(suite)
710   return results.wasSuccessful()
711
712
713 if __name__ == '__main__':
714   parser = optparse.OptionParser()
715   info = ('The build directory for xwalk.'
716           'Such as: --build-dir=src/out')
717   parser.add_option('--build-dir', help=info)
718   info = ('The build target for xwalk.'
719           'Such as: --target=Release')
720   parser.add_option('--target', help=info)
721   info = ('The path of package tool.')
722   parser.add_option('--tool-path', help=info)
723   info = ('The packaging mode for xwalk. Such as: --mode=embedded.'
724           'Please refer the detail to the option of make_apk.py.')
725   parser.add_option('--mode', help=info)
726   options, dummy = parser.parse_args()
727   if len(sys.argv) == 1:
728     parser.print_help()
729     sys.exit(1)
730
731   del sys.argv[1:]
732   mode_suite = SuiteWithModeOption()
733   empty_mode_suite = SuiteWithEmptyModeOption()
734   runner = unittest.TextTestRunner(verbosity=2)
735   if options.build_dir or options.target:
736     warnings.warn(('"--build-dir" and "--target" will be deprecated soon, '
737                    'please leverage "--tool-path" instead.'),
738                   Warning)
739   test_result = True
740   if options.mode:
741     test_result = TestSuiteRun(runner, mode_suite)
742   else:
743     # Run tests in both embedded and shared mode
744     # when the mode option isn't specified.
745     options.mode = 'embedded'
746     print 'Run tests in embedded mode.'
747     test_result = TestSuiteRun(runner, mode_suite)
748     options.mode = 'shared'
749     print 'Run tests in shared mode.'
750     test_result = TestSuiteRun(runner, mode_suite) and test_result
751     print 'Run test without \'--mode\' option.'
752     test_result = TestSuiteRun(runner, empty_mode_suite) and test_result
753   if not test_result:
754     sys.exit(1)