Upstream version 10.38.217.0
[platform/framework/web/crosswalk.git] / src / xwalk / app / tools / android / make_apk_test.py
1 #!/usr/bin/env python
2 # coding: UTF-8
3
4 # Copyright (c) 2013, 2014 Intel Corporation. All rights reserved.
5 # Use of this source code is governed by a BSD-style license that can be
6 # found in the LICENSE file.
7
8 import optparse
9 import os
10 import shutil
11 import subprocess
12 import sys
13 import unittest
14 import warnings
15
16
17 def Clean(name, app_version):
18   if os.path.exists(name):
19     shutil.rmtree(name)
20   if options.mode == 'shared':
21     if os.path.isfile(name + '_' + app_version + '.apk'):
22       os.remove(name + '_' + app_version + '.apk')
23   else:
24     if os.path.isfile(name + '_' + app_version + '_x86.apk'):
25       os.remove(name + '_' + app_version + '_x86.apk')
26     if os.path.isfile(name + '_' + app_version + '_arm.apk'):
27       os.remove(name + '_' + app_version + '_arm.apk')
28
29
30 def CompareSizeForCompressor(mode, original, ext, name, fun):
31   size = 0
32   compressed_size = 0
33   mode_list = ['all', 'js', 'css']
34
35   www_dir = os.path.join(name, 'assets', 'www')
36   if os.path.exists(www_dir):
37     size = GetFileSize(original)
38     compressed_file = os.path.join(www_dir, ext, 'test.' + ext)
39     compressed_size = GetFileSize(compressed_file)
40
41     if mode in mode_list:
42       fun(compressed_size < size)
43     else:
44       fun(size == compressed_size)
45   else:
46     print('Error: %s is not exist.' % www_dir)
47
48
49 def GetFileSize(file_path):
50   size = 0
51   if os.path.exists(file_path):
52     size = os.path.getsize(file_path)
53   return size
54
55
56 def RunCommand(command):
57   """Runs the command list, return the output."""
58   proc = subprocess.Popen(command, stdout=subprocess.PIPE,
59                           stderr=subprocess.STDOUT, shell=False)
60   return proc.communicate()[0]
61
62
63 def GetResultWithOption(mode=None, manifest=None, name=None, package=None):
64   app_url = None
65   if manifest is not None:
66     manifest = '--manifest=' + manifest
67   else:
68     app_url = '--app-url=http://www.intel.com'
69   if name is not None:
70     name = '--name=' + name
71   if package is not None:
72     package = '--package=' + package
73
74   cmd = ['python', 'make_apk.py',
75          '--app-version=1.0.0',
76          '%s' % manifest,
77          '%s' % name,
78          '%s' % package,
79          '%s' % app_url,
80          '%s' % mode]
81   return RunCommand(cmd)
82
83
84 class TestMakeApk(unittest.TestCase):
85   @classmethod
86   def setUpClass(cls):
87     cls._original_dir = os.getcwd()
88     if options.tool_path:
89       target_dir = os.path.expanduser(options.tool_path)
90     elif options.build_dir and options.target:
91       target_dir = os.path.join(options.build_dir,
92                                 options.target,
93                                 'xwalk_app_template')
94     if os.path.exists(target_dir):
95       # Prepare the test data.
96       test_src_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)),
97                                   'test_data')
98       test_des_dir = os.path.join(target_dir, 'test_data')
99       if not os.path.exists(test_des_dir):
100         shutil.copytree(test_src_dir, test_des_dir)
101       os.chdir(target_dir)
102     else:
103       unittest.SkipTest('xwalk_app_template folder doesn\'t exist. '
104                         'Skipping all tests in make_apk_test.py')
105     cls._mode = ''
106     if options.mode == 'shared':
107       cls._mode = '--mode=shared'
108     elif options.mode == 'embedded':
109       cls._mode = '--mode=embedded'
110     cls.fakeNativeLibrary()
111
112   @classmethod
113   def tearDownClass(cls):
114     # Clean the test data.
115     if options.tool_path:
116       test_data_dir = os.path.join(os.path.expanduser(options.tool_path),
117                                    'test_data')
118     elif options.build_dir and options.target:
119       test_data_dir = os.path.join(options.build_dir,
120                                    options.target,
121                                    'xwalk_app_template',
122                                    'test_data')
123     if os.path.exists(test_data_dir):
124       shutil.rmtree(test_data_dir)
125     cls.restoreNativeLibrary()
126     os.chdir(cls._original_dir)
127
128   @staticmethod
129   def fakeNativeLibrary():
130     # To reduce the time consumption of make_apk test for embedded mode,
131     # replace the original native library with an empty library.
132     # Because it doesn't affect the result of test.
133     if options.mode == 'embedded':
134       native_library_dir = os.path.join('xwalk_core_library', 'libs')
135       native_library_temp_dir = 'temp'
136       Clean('temp', '0') # May be left over from aborted tests
137       shutil.copytree(native_library_dir, native_library_temp_dir)
138       for root, _, files in os.walk(native_library_dir):
139         if 'libxwalkcore.so' in files:
140           native_library_path = os.path.join(root, 'libxwalkcore.so')
141           # Remove the original library
142           os.remove(native_library_path)
143           # Create an empty library file
144           open(native_library_path, 'a').close()
145
146   @staticmethod
147   def restoreNativeLibrary():
148     # Restore the original native library for embedded mode.
149     if options.mode == 'embedded':
150       native_library_dir = os.path.join('xwalk_core_library', 'libs')
151       native_library_temp_dir = 'temp'
152       if os.path.isdir(native_library_dir):
153         shutil.rmtree(native_library_dir)
154       shutil.move(native_library_temp_dir, native_library_dir)
155
156   @staticmethod
157   def archs():
158     x86_native_lib_path = os.path.join('xwalk_core_library', 'libs',
159                                        'x86', 'libxwalkcore.so')
160     arm_native_lib_path = os.path.join('xwalk_core_library', 'libs',
161                                        'armeabi-v7a', 'libxwalkcore.so')
162     arch_list = []
163     if os.path.isfile(x86_native_lib_path):
164       arch_list.append('x86')
165     if os.path.isfile(arm_native_lib_path):
166       arch_list.append('arm')
167     return arch_list
168
169   def checkApks(self, apk_name, app_version, keystore_path=None):
170     apks = []
171     if self._mode.find('shared') != -1:
172       apk_path = '%s_%s.apk' % (apk_name, app_version)
173       apks.append((apk_path, ''))
174     elif self._mode.find('embedded') != -1:
175       x86_apk_path = '%s_%s_x86.apk' % (apk_name, app_version)
176       if os.path.exists(x86_apk_path):
177         apks.append((x86_apk_path, 'x86'))
178       arm_apk_path = '%s_%s_arm.apk' % (apk_name, app_version)
179       if os.path.exists(arm_apk_path):
180         apks.append((arm_apk_path, 'arm'))
181
182     for apk, apk_arch in apks:
183       self.checkApk(apk, apk_arch, keystore_path)
184
185   def checkApk(self, apk_path, arch, keystore_path=None):
186     """Checks whether some files are contained in the given APK,
187     and optionally verifies its signature."""
188     cmd = ['jar', 'tvf', apk_path]
189     out = RunCommand(cmd)
190     common_files = ['AndroidManifest.xml', 'classes.dex']
191     for res_file in common_files:
192       self.assertTrue(out.find(res_file) != -1)
193     if self._mode.find('embedded') != -1:
194       embedded_related_files = ['icudtl.dat',
195                                 'xwalk.pak',
196                                 'device_capabilities_api.js',
197                                 'launch_screen_api.js',
198                                 'presentation_api.js']
199       for res_file in embedded_related_files:
200         self.assertTrue(out.find(res_file) != -1)
201     if arch == 'x86':
202       self.assertTrue(out.find('x86/libxwalkcore.so') != -1)
203     elif arch == 'arm':
204       self.assertTrue(out.find('armeabi-v7a/libxwalkcore.so') != -1)
205
206     if keystore_path:
207       cmd = ['jarsigner', '-verify', '-keystore', keystore_path,
208              '-verbose', apk_path]
209       out = RunCommand(cmd)
210       self.assertIn('smk', out)
211
212   def testName(self):
213     cmd = ['python', 'make_apk.py', '--app-version=1.0.0',
214            '--app-url=http://www.intel.com',
215            '--package=org.xwalk.example', self._mode]
216     out = RunCommand(cmd)
217     Clean('Example', '1.0.0')
218     self.assertIn('An APK name is required', out)
219
220     cmd = ['python', 'make_apk.py', '--name=Test_Example',
221            '--app-version=1.0.0', '--app-url=http://www.intel.com',
222            '--package=org.xwalk.example', self._mode]
223     out = RunCommand(cmd)
224     self.assertNotIn('An APK name is required', out)
225     Clean('Example', '1.0.0')
226
227   def testToolVersion(self):
228     cmd = ['python', 'make_apk.py', '--version']
229     out = RunCommand(cmd)
230     self.assertTrue(out.find('Crosswalk app packaging tool version') != -1)
231
232   def testAppDescriptionAndVersion(self):
233     cmd = ['python', 'make_apk.py', '--name=Example',
234            '--package=org.xwalk.example', '--app-version=1.0.0',
235            '--description=a sample application',
236            '--app-url=http://www.intel.com',
237            '--project-dir=.', self._mode]
238     RunCommand(cmd)
239     self.addCleanup(Clean, 'Example', '1.0.0')
240     manifest = 'Example/AndroidManifest.xml'
241     with open(manifest, 'r') as content_file:
242       content = content_file.read()
243     self.assertTrue(os.path.exists(manifest))
244     self.assertTrue(content.find('description') != -1)
245     self.assertTrue(content.find('versionName') != -1)
246     self.checkApks('Example', '1.0.0')
247
248   def testAppVersionCode(self):
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-versionCode=3',
253            '--app-url=http://www.intel.com',
254            '--project-dir=.',
255            self._mode]
256     RunCommand(cmd)
257     self.addCleanup(Clean, 'Example', '1.0.0')
258     manifest = 'Example/AndroidManifest.xml'
259     with open(manifest, 'r') as content_file:
260       content = content_file.read()
261     self.assertTrue(os.path.exists(manifest))
262     self.assertTrue(content.find('versionCode="3"') != -1)
263     self.checkApks('Example', '1.0.0')
264
265   def testAppVersionCodeBase(self):
266     # Arch option only works for embedded mode,
267     # so only test it for embedded mode.
268     if self._mode.find('embedded') == -1:
269       return
270     if 'x86' in self.archs():
271       arch = '--arch=x86'
272       versionCode = 'versionCode="60000003"'
273     else:
274       arch = '--arch=arm'
275       versionCode = 'versionCode="20000003"'
276     cmd = ['python', 'make_apk.py', '--name=Example',
277            '--package=org.xwalk.example', '--app-version=1.0.0',
278            '--description=a sample application',
279            '--app-versionCodeBase=3',
280            arch,
281            '--app-url=http://www.intel.com',
282            '--project-dir=.',
283            self._mode]
284     RunCommand(cmd)
285     self.addCleanup(Clean, 'Example', '1.0.0')
286     manifest = 'Example/AndroidManifest.xml'
287     with open(manifest, 'r') as content_file:
288       content = content_file.read()
289     self.assertTrue(os.path.exists(manifest))
290     self.assertTrue(content.find(versionCode) != -1)
291     self.checkApks('Example', '1.0.0')
292
293   def testAppBigVersionCodeBase(self):
294     # Arch option only works for embedded mode,
295     # so only test it for embedded mode.
296     if self._mode.find('embedded') == -1:
297       return
298     if 'x86' in self.archs():
299       arch = '--arch=x86'
300     else:
301       arch = '--arch=arm'
302     cmd = ['python', 'make_apk.py', '--name=Example',
303            '--package=org.xwalk.example', '--app-version=1.0.0',
304            '--description=a sample application',
305            '--app-versionCodeBase=30000000',
306            arch,
307            '--app-url=http://www.intel.com',
308            '--project-dir=.',
309            self._mode]
310     RunCommand(cmd)
311     self.addCleanup(Clean, 'Example', '1.0.0')
312     manifest = 'Example/AndroidManifest.xml'
313     self.assertFalse(os.path.exists(manifest))
314
315   def testPermissions(self):
316     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
317            '--package=org.xwalk.example', '--permissions=geolocation',
318            '--app-url=http://www.intel.com',
319            '--project-dir=.',
320            self._mode]
321     RunCommand(cmd)
322     self.addCleanup(Clean, 'Example', '1.0.0')
323     manifest = 'Example/AndroidManifest.xml'
324     with open(manifest, 'r') as content_file:
325       content = content_file.read()
326     self.assertTrue(os.path.exists(manifest))
327     self.assertTrue(content.find('ACCESS_FINE_LOCATION') != -1)
328     self.checkApks('Example', '1.0.0')
329
330   def testPermissionsWithError(self):
331     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
332            '--package=org.xwalk.example', '--permissions=UndefinedPermission',
333            '--app-url=http://www.intel.com', self._mode]
334     out = RunCommand(cmd)
335     self.assertTrue(out.find('related API is not supported.') != -1)
336     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
337            '--package=org.xwalk.example',
338            '--permissions=Contacts.Geolocation.Messaging',
339            '--app-url=http://www.intel.com', self._mode]
340     out = RunCommand(cmd)
341     self.assertTrue(out.find('related API is not supported.') != -1)
342
343   def testPackage(self):
344     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
345            '--app-url=http://www.intel.com', self._mode]
346     out = RunCommand(cmd)
347     self.assertIn('A package name is required', out)
348
349     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
350            '--package=org.xwalk.example',
351            '--app-url=http://www.intel.com', self._mode]
352     out = RunCommand(cmd)
353     self.assertNotIn('A package name is required', out)
354     Clean('Example', '1.0.0')
355
356   def testPackageWithInvalidCharacter(self):
357     package_name_error = 'package name should be started with letters'
358
359     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
360            '--package=org.xwalk._example',
361            '--app-url=http://www.intel.com', self._mode]
362     out = RunCommand(cmd)
363     self.assertIn(package_name_error, out)
364
365     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
366            '--package=org.xwalk.123example',
367            '--app-url=http://www.intel.com', self._mode]
368     out = RunCommand(cmd)
369     self.assertIn(package_name_error, out)
370
371     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
372            '--package=org.xwalk.example_',
373            '--app-url=http://www.intel.com', self._mode]
374     out = RunCommand(cmd)
375     self.assertNotIn(package_name_error, out)
376     Clean('Example', '1.0.0')
377
378   def testEntry(self):
379     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
380            '--package=org.xwalk.example', '--app-url=http://www.intel.com',
381            self._mode]
382     out = RunCommand(cmd)
383     self.assertNotIn('You must pass either "--app-url" or',
384                      out)
385     self.assertNotIn('You must specify both "--app-local-path" and',
386                      out)
387     self.addCleanup(Clean, 'Example', '1.0.0')
388     self.checkApks('Example', '1.0.0')
389     Clean('Example', '1.0.0')
390
391     test_entry_root = 'test_data/entry'
392     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
393            '--package=org.xwalk.example', '--app-root=%s' % test_entry_root,
394            '--app-local-path=index.html', self._mode]
395     out = RunCommand(cmd)
396     self.assertNotIn('You must pass either "--app-url" or',
397                      out)
398     self.assertNotIn('You must specify both "--app-local-path" and',
399                      out)
400     self.checkApks('Example', '1.0.0')
401
402   def testEntryWithErrors(self):
403     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
404            '--package=org.xwalk.example', self._mode]
405     out = RunCommand(cmd)
406     self.addCleanup(Clean, 'Example', '1.0.0')
407     self.assertIn('You must pass either "--app-url" or',
408                   out)
409
410     self.assertFalse(os.path.exists('Example.apk'))
411     Clean('Example', '1.0.0')
412
413     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
414            '--package=org.xwalk.example', '--app-url=http://www.intel.com',
415            '--app-root=.', self._mode]
416     out = RunCommand(cmd)
417     self.assertIn('You must pass either "--app-url" or',
418                   out)
419     self.assertFalse(os.path.exists('Example.apk'))
420     Clean('Example', '1.0.0')
421
422     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
423            '--package=org.xwalk.example', '--app-root=./', self._mode]
424     out = RunCommand(cmd)
425     self.assertIn('You must specify both "--app-local-path" and',
426                   out)
427     self.assertFalse(os.path.exists('Example.apk'))
428     Clean('Example', '1.0.0')
429
430     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
431            '--package=org.xwalk.example', '--app-local-path=index.html',
432            self._mode]
433     out = RunCommand(cmd)
434     self.assertIn('You must specify both "--app-local-path" and',
435                   out)
436     self.assertFalse(os.path.exists('Example.apk'))
437     Clean('Example', '1.0.0')
438
439     manifest_path = os.path.join('test_data', 'manifest',
440                                  'manifest_app_launch_local_path.json')
441     cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
442            '--manifest=%s' % manifest_path, self._mode]
443     out = RunCommand(cmd)
444     self.assertTrue(
445         out.find('Please make sure that the local path file') != -1)
446     self.assertFalse(os.path.exists('Example.apk'))
447
448   def testIconByOption(self):
449     icon = os.path.join('test_data', 'manifest', 'icons', 'icon_96.png')
450     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
451            '--package=org.xwalk.example', '--app-url=http://www.intel.com',
452            '--icon=%s' % icon, '--project-dir=.', self._mode]
453     RunCommand(cmd)
454     self.addCleanup(Clean, 'Example', '1.0.0')
455     manifest = 'Example/AndroidManifest.xml'
456     with open(manifest, 'r') as content_file:
457       content = content_file.read()
458     self.assertTrue(content.find('drawable/icon_96') != -1)
459     self.checkApks('Example', '1.0.0')
460
461   def testIconByManifest(self):
462     manifest_path = os.path.join('test_data', 'manifest', 'manifest_icon.json')
463     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
464            '--package=org.xwalk.example', '--app-url=http://www.intel.com',
465            '--manifest=%s' % manifest_path, '--project-dir=.', self._mode]
466     RunCommand(cmd)
467     self.addCleanup(Clean, 'Example', '1.0.0')
468     manifest = 'Example/AndroidManifest.xml'
469     with open(manifest, 'r') as content_file:
470       content = content_file.read()
471     self.assertTrue(content.find('drawable/icon') != -1)
472     self.checkApks('Example', '1.0.0')
473
474   def testFullscreen(self):
475     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
476            '--package=org.xwalk.example', '--app-url=http://www.intel.com',
477            '-f', '--project-dir=.', self._mode]
478     RunCommand(cmd)
479     self.addCleanup(Clean, 'Example', '1.0.0')
480     theme = 'Example/res/values-v14/theme.xml'
481     with open(theme, 'r') as content_file:
482       content = content_file.read()
483     self.assertTrue(os.path.exists(theme))
484     self.assertTrue(
485         content.find(
486             '<item name="android:windowFullscreen">true</item>') != -1)
487     self.checkApks('Example', '1.0.0')
488
489   def testEnableRemoteDebugging(self):
490     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
491            '--package=org.xwalk.example', '--app-url=http://www.intel.com',
492            '--enable-remote-debugging', '--project-dir=.', self._mode]
493     RunCommand(cmd)
494     self.addCleanup(Clean, 'Example', '1.0.0')
495     activity = 'Example/src/org/xwalk/example/ExampleActivity.java'
496     with open(activity, 'r') as content_file:
497       content = content_file.read()
498     self.assertTrue(os.path.exists(activity))
499     self.assertTrue(content.find('setRemoteDebugging') != -1)
500     self.checkApks('Example', '1.0.0')
501     Clean('Example', '1.0.0')
502     manifest_path = os.path.join('test_data', 'manifest', 'manifest.json')
503     cmd = ['python', 'make_apk.py', '--enable-remote-debugging',
504            '--package=org.xwalk.example',
505            '--manifest=%s' % manifest_path,
506            '--project-dir=.',
507            self._mode]
508     RunCommand(cmd)
509     activity = 'Example/src/org/xwalk/example/ExampleActivity.java'
510     with open(activity, 'r') as content_file:
511       content = content_file.read()
512     self.assertTrue(os.path.exists(activity))
513     self.assertTrue(content.find('setRemoteDebugging') != -1)
514     self.checkApks('Example', '1.0.0')
515
516   def testUseAnimatableView(self):
517     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
518            '--package=org.xwalk.example', '--app-url=http://www.intel.com',
519            '--use-animatable-view', self._mode]
520     RunCommand(cmd)
521     self.addCleanup(Clean, 'Example', '1.0.0')
522     activity = 'Example/src/org/xwalk/example/ExampleActivity.java'
523     with open(activity, 'r') as content_file:
524       content = content_file.read()
525     self.assertTrue(os.path.exists(activity))
526     self.assertTrue(content.find('setUseAnimatableView') != -1)
527     self.checkApks('Example', '1.0.0')
528     Clean('Example', '1.0.0')
529     manifest_path = os.path.join('test_data', 'manifest', 'manifest.json')
530     cmd = ['python', 'make_apk.py', '--use-animatable-view',
531            '--package=org.xwalk.example',
532            '--manifest=%s' % manifest_path, self._mode]
533     RunCommand(cmd)
534     activity = 'Example/src/org/xwalk/example/ExampleActivity.java'
535     with open(activity, 'r') as content_file:
536       content = content_file.read()
537     self.assertTrue(os.path.exists(activity))
538     self.assertTrue(content.find('setUseAnimatableView') != -1)
539     self.checkApks('Example', '1.0.0')
540
541   def testKeystore(self):
542     keystore_path = os.path.join('test_data', 'keystore',
543                                  'xwalk-test.keystore')
544     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
545            '--package=org.xwalk.example', '--app-url=http://www.intel.com',
546            '--keystore-path=%s' % keystore_path, '--keystore-alias=xwalk-test',
547            '--keystore-passcode=xwalk-test',
548            '--keystore-alias-passcode=xwalk-test',
549            '--project-dir=.', self._mode]
550     RunCommand(cmd)
551     self.assertTrue(os.path.isdir('Example'))
552     self.checkApks('Example', '1.0.0', keystore_path)
553     Clean('Example', '1.0.0')
554
555     keystore_path_with_space = os.path.join('test_data', 'keystore',
556                                             'test keystore')
557     shutil.copy2(keystore_path, keystore_path_with_space)
558     keystore_path = os.path.join('test_data', 'keystore',
559                                  'xwalk-test.keystore')
560     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
561            '--package=org.xwalk.example', '--app-url=http://www.intel.com',
562            '--keystore-path=%s' % keystore_path_with_space,
563            '--keystore-alias=xwalk test',
564            '--keystore-passcode=xwalk-test',
565            '--keystore-alias-passcode=xwalk test',
566            '--project-dir=.', self._mode]
567     RunCommand(cmd)
568     self.assertTrue(os.path.isdir('Example'))
569     self.checkApks('Example', '1.0.0', keystore_path_with_space)
570     Clean('Example', '1.0.0')
571
572   def testManifest(self):
573     manifest_path = os.path.join('test_data', 'manifest', 'manifest.json')
574     cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
575            '--manifest=%s' % manifest_path, '--project-dir=.', self._mode]
576     RunCommand(cmd)
577     self.addCleanup(Clean, 'Example', '1.0.0')
578     manifest = 'Example/AndroidManifest.xml'
579     with open(manifest, 'r') as content_file:
580       content = content_file.read()
581     self.assertTrue(os.path.exists(manifest))
582     self.assertTrue(content.find('android.permission.READ_CONTACTS') != -1)
583     self.assertTrue(content.find('android.permission.WRITE_CONTACTS') != -1)
584     self.assertTrue(
585         content.find('android.permission.ACCESS_FINE_LOCATION') != -1)
586     self.assertTrue(content.find('android.permission.READ_SMS') != -1)
587     self.assertTrue(content.find('android.permission.RECEIVE_SMS') != -1)
588     self.assertTrue(content.find('android.permission.SEND_SMS') != -1)
589     self.assertTrue(content.find('android.permission.WRITE_SMS') != -1)
590     self.assertTrue(content.find('landscape') != -1)
591     theme = 'Example/res/values-v14/theme.xml'
592     with open(theme, 'r') as content_file:
593       content = content_file.read()
594     self.assertTrue(os.path.exists(theme))
595     self.assertTrue(
596         content.find(
597             '<item name="android:windowFullscreen">true</item>') != -1)
598     self.assertTrue(os.path.exists('Example'))
599     self.checkApks('Example', '1.0.0')
600
601   def testManifestWithSpecificValue(self):
602     manifest_path = os.path.join('test_data', 'manifest',
603                                  'manifest_app_launch_local_path.json')
604     cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
605            '--manifest=%s' % manifest_path, '--project-dir=.', self._mode]
606     out = RunCommand(cmd)
607     self.addCleanup(Clean, 'Example', '1.0.0')
608     self.assertTrue(out.find('no app launch path') == -1)
609     self.checkApks('Example', '1.0.0')
610
611   def testManifestWithDeprecatedField(self):
612     manifest_path = os.path.join('test_data', 'manifest', 'deprecated',
613                                  'manifest_app_local_path.json')
614     cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
615            '--manifest=%s' % manifest_path, self._mode]
616     out = RunCommand(cmd)
617     self.addCleanup(Clean, 'Example', '1.0.0')
618     self.assertIn('Warning: The following fields have been deprecated', out)
619     self.assertIn('app.launch.local_path', out)
620     Clean('Example', '1.0.0')
621
622     manifest_path = os.path.join('test_data', 'manifest', 'deprecated',
623                                  'manifest_launch_path.json')
624     cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
625            '--manifest=%s' % manifest_path, self._mode]
626     out = RunCommand(cmd)
627     self.assertIn('Warning: The following fields have been deprecated', out)
628     self.assertIn('launch_path', out)
629     Clean('Example', '1.0.0')
630
631     manifest_path = os.path.join('test_data', 'manifest', 'deprecated',
632                                  'manifest_permissions.json')
633     cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
634            '--manifest=%s' % manifest_path, self._mode]
635     out = RunCommand(cmd)
636     self.assertIn('Warning: The following fields have been deprecated', out)
637     self.assertIn('permissions', out)
638     Clean('Example', '1.0.0')
639
640     manifest_path = os.path.join('test_data', 'manifest',
641                                  'manifest_deprecated_icon.json')
642     cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
643            '--manifest=%s' % manifest_path, self._mode]
644     out = RunCommand(cmd)
645     self.assertIn('Warning: The following fields have been deprecated', out)
646     self.assertIn('icons', out)
647     Clean('Example', '1.0.0')
648
649     manifest_path = os.path.join('test_data', 'manifest', 'deprecated',
650                                  'manifest_description.json')
651     cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
652            '--manifest=%s' % manifest_path, self._mode]
653     out = RunCommand(cmd)
654     self.assertIn('Warning: The following fields have been deprecated', out)
655     self.assertIn('description', out)
656     Clean('Example', '1.0.0')
657
658     manifest_path = os.path.join('test_data', 'manifest', 'deprecated',
659                                  'manifest_deprecated_version.json')
660     cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
661            '--manifest=%s' % manifest_path, self._mode]
662     out = RunCommand(cmd)
663     self.assertIn('Warning: The following fields have been deprecated', out)
664     self.assertIn('version', out)
665
666   def testManifestWithError(self):
667     manifest_path = os.path.join('test_data', 'manifest',
668                                  'manifest_no_app_launch_path.json')
669     cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
670            '--manifest=%s' % manifest_path, '--verbose', self._mode]
671     out = RunCommand(cmd)
672     self.assertTrue(out.find('no app launch path') != -1)
673     manifest_path = os.path.join('test_data', 'manifest',
674                                  'manifest_no_name.json')
675     cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
676            '--manifest=%s' % manifest_path, '--verbose', self._mode]
677     out = RunCommand(cmd)
678     self.assertTrue(out.find('no \'name\' field') != -1)
679     manifest_path = os.path.join('test_data', 'manifest',
680                                  'manifest_permissions_format_error.json')
681     cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
682            '--manifest=%s' % manifest_path, '--verbose', self._mode]
683     out = RunCommand(cmd)
684     self.assertTrue(out.find('\'Permissions\' field error') != -1)
685     manifest_path = os.path.join('test_data', 'manifest',
686                                  'manifest_permissions_field_error.json')
687     cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
688            '--manifest=%s' % manifest_path, '--verbose', self._mode]
689     out = RunCommand(cmd)
690     self.assertTrue(out.find('\'Permissions\' field error') != -1)
691     manifest_path = os.path.join('test_data', 'manifest',
692                                  'manifest_not_supported_permission.json')
693     cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
694            '--manifest=%s' % manifest_path, '--verbose', self._mode]
695     out = RunCommand(cmd)
696     self.assertTrue(
697         out.find('\'Telephony\' related API is not supported') != -1)
698
699   def testExtensionsWithOneExtension(self):
700     # Test with an existed extension.
701     extension_path = 'test_data/extensions/myextension'
702     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
703            '--package=org.xwalk.example', '--app-url=http://www.intel.com',
704            '--extensions=%s' % extension_path, '--project-dir=.', self._mode]
705     RunCommand(cmd)
706     self.addCleanup(Clean, 'Example', '1.0.0')
707     self.assertTrue(os.path.exists('Example'))
708     extensions_config_json = 'Example/assets/extensions-config.json'
709     self.assertTrue(os.path.exists(extensions_config_json))
710     with open(extensions_config_json, 'r') as content_file:
711       content = content_file.read()
712     self.assertTrue(
713         content.find('xwalk-extensions/myextension/myextension.js'))
714     self.assertTrue(content.find('com.example.extension.MyExtension'))
715     extension_js = 'Example/assets/xwalk-extensions/myextension/myextension.js'
716     self.assertTrue(os.path.exists(extension_js))
717     extension_jar = 'Example/xwalk-extensions/myextension/myextension.jar'
718     self.assertTrue(os.path.exists(extension_jar))
719     self.checkApks('Example', '1.0.0')
720
721   def testExtensionsWithNonExtension(self):
722     # Test with a non-existing extension.
723     extension_path = 'test_data/extensions/myextension'
724     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
725            '--package=org.xwalk.example', '--app-url=http://www.intel.com',
726            '--extensions=%s1' % extension_path, self._mode, '--verbose']
727     out = RunCommand(cmd)
728     self.assertTrue(out.find('Exiting with error code: 9') != -1)
729
730   def testExtensionWithPermissions(self):
731     test_entry_root = 'test_data/entry'
732     # Add redundant separators for test.
733     extension_path = 'test_data//extensions/contactextension/'
734     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
735            '--package=org.xwalk.example', '--app-root=%s' % test_entry_root,
736            '--app-local-path=contactextension.html',
737            '--extensions=%s' % extension_path,
738            '--project-dir=.', self._mode]
739     RunCommand(cmd)
740     self.addCleanup(Clean, 'Example', '1.0.0')
741     self.assertTrue(os.path.exists('Example'))
742     manifest = 'Example/AndroidManifest.xml'
743     with open(manifest, 'r') as content_file:
744       content = content_file.read()
745     self.assertTrue(os.path.exists(manifest))
746     self.assertTrue(content.find('android.permission.WRITE_CONTACTS') != -1)
747     self.assertTrue(content.find('android.permission.READ_CONTACTS') != -1)
748     self.checkApks('Example', '1.0.0')
749
750   def testXPK(self):
751     xpk_file = os.path.join('test_data', 'xpk', 'example.xpk')
752     cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
753            '--xpk=%s' % xpk_file, '--project-dir=.', self._mode]
754     RunCommand(cmd)
755     self.addCleanup(Clean, 'Example', '1.0.0')
756     self.assertTrue(os.path.exists('Example'))
757     self.checkApks('Example', '1.0.0')
758
759   def testXPKWithError(self):
760     xpk_file = os.path.join('test_data', 'xpk', 'error.xpk')
761     cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
762            '--xpk=%s' % xpk_file, '--project-dir=.', self._mode]
763     out = RunCommand(cmd)
764     error_msg = 'XPK doesn\'t contain manifest file'
765     self.assertTrue(out.find(error_msg) != -1)
766     self.assertFalse(os.path.exists('Example'))
767
768   def testOrientation(self):
769     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
770            '--package=org.xwalk.example', '--app-url=http://www.intel.com',
771            '--orientation=landscape', '--project-dir=.', self._mode]
772     RunCommand(cmd)
773     self.addCleanup(Clean, 'Example', '1.0.0')
774     manifest = 'Example/AndroidManifest.xml'
775     with open(manifest, 'r') as content_file:
776       content = content_file.read()
777     self.assertTrue(os.path.exists(manifest))
778     self.assertTrue(content.find('landscape') != -1)
779     self.assertTrue(os.path.exists('Example'))
780     self.checkApks('Example', '1.0.0')
781
782   def testArch(self):
783     # Arch option only works for embedded mode,
784     # so only test it for embedded mode.
785     if self._mode.find('embedded') != -1:
786       cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
787              '--package=org.xwalk.example', '--app-url=http://www.intel.com',
788              '--arch=x86', self._mode]
789       RunCommand(cmd)
790       self.addCleanup(Clean, 'Example', '1.0.0')
791       if 'x86' in self.archs():
792         self.assertTrue(os.path.isfile('Example_1.0.0_x86.apk'))
793         self.checkApk('Example_1.0.0_x86.apk', 'x86')
794       else:
795         self.assertFalse(os.path.isfile('Example_1.0.0_x86.apk'))
796       self.assertFalse(os.path.isfile('Example_1.0.0_arm.apk'))
797       Clean('Example', '1.0.0')
798       cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
799              '--package=org.xwalk.example', '--app-url=http://www.intel.com',
800              '--arch=arm', self._mode]
801       RunCommand(cmd)
802       if 'arm' in self.archs():
803         self.assertTrue(os.path.isfile('Example_1.0.0_arm.apk'))
804         self.checkApk('Example_1.0.0_arm.apk', 'arm')
805       else:
806         self.assertFalse(os.path.isfile('Example_1.0.0._arm.apk'))
807       self.assertFalse(os.path.isfile('Example_1.0.0_x86.apk'))
808       Clean('Example', '1.0.0')
809       cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
810              '--package=org.xwalk.example', '--app-url=http://www.intel.com',
811              self._mode]
812       RunCommand(cmd)
813       if 'arm' in self.archs():
814         self.assertTrue(os.path.isfile('Example_1.0.0_arm.apk'))
815         self.checkApk('Example_1.0.0_arm.apk', 'arm')
816       else:
817         self.assertFalse(os.path.isfile('Example_1.0.0._arm.apk'))
818       if 'x86' in self.archs():
819         self.assertTrue(os.path.isfile('Example_1.0.0_x86.apk'))
820         self.checkApk('Example_1.0.0_x86.apk', 'x86')
821       else:
822         self.assertFalse(os.path.isfile('Example_1.0.0._x86.apk'))
823       Clean('Example', '1.0.0')
824       cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
825              '--package=org.xwalk.example', '--app-url=http://www.intel.com',
826              '--arch=undefined', self._mode]
827       out = RunCommand(cmd)
828       error_msg = 'invalid choice: \'undefined\''
829       self.assertTrue(out.find(error_msg) != -1)
830
831   def testVerbose(self):
832     cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
833            '--package=org.xwalk.example', '--app-url=http://www.intel.com',
834            '--verbose', '--project-dir=.', self._mode]
835     result = RunCommand(cmd)
836     self.addCleanup(Clean, 'Example', '1.0.0')
837     self.assertTrue(result.find('aapt') != -1)
838     self.assertTrue(result.find('crunch') != -1)
839     self.assertTrue(result.find('apkbuilder') != -1)
840     self.assertTrue(os.path.exists('Example'))
841     self.checkApks('Example', '1.0.0')
842
843   def executeCommandAndVerifyResult(self, exec_file):
844     # Test all of supported options with empty 'mode' option.
845     icon_path = './template/res/drawable-xhdpi/crosswalk.png'
846     extension_path = 'test_data/extensions/myextension'
847     arch = ''
848     icon = ''
849     if exec_file.find("make_apk.py") != -1:
850       arch = '--arch=x86'
851       icon = '--icon=%s' % icon_path
852     cmd = ['python', '%s' % exec_file,
853            '--app-version=1.0.0',
854            '--app-url=http://www.intel.com',
855            '%s' % arch,
856            '--description=a sample application',
857            '--enable-remote-debugging',
858            '--extensions=%s' % extension_path,
859            '--fullscreen',
860            '--keep-screen-on',
861            '%s' % icon,
862            '--name=Example',
863            '--orientation=landscape',
864            '--package=org.xwalk.example',
865            '--permissions=geolocation',
866            '--project-dir=.']
867     RunCommand(cmd)
868     self.addCleanup(Clean, 'Example', '1.0.0')
869     activity = 'Example/src/org/xwalk/example/ExampleActivity.java'
870     with open(activity, 'r') as content_file:
871       content = content_file.read()
872     self.assertTrue(os.path.exists(activity))
873     # Test remote debugging option.
874     self.assertTrue(content.find('setRemoteDebugging') != -1)
875     # Test keep screen on option
876     self.assertTrue(content.find('FLAG_KEEP_SCREEN_ON') != -1)
877
878     manifest = 'Example/AndroidManifest.xml'
879     with open(manifest, 'r') as content_file:
880       content = content_file.read()
881     self.assertTrue(os.path.exists(manifest))
882     # Test permission option.
883     self.assertTrue(content.find('ACCESS_FINE_LOCATION') != -1)
884     # Test description option.
885     self.assertTrue(content.find('description') != -1)
886     # Test app version option.
887     self.assertTrue(content.find('versionName') != -1)
888     # Test orientation option.
889     self.assertTrue(content.find('landscape') != -1)
890     # Test fullscreen option
891     theme = 'Example/res/values-v14/theme.xml'
892     with open(theme, 'r') as content_file:
893       content = content_file.read()
894     self.assertTrue(os.path.exists(theme))
895     self.assertTrue(
896         content.find(
897             '<item name="android:windowFullscreen">true</item>') != -1)
898     # Test extensions option.
899     extensions_config_json = 'Example/assets/extensions-config.json'
900     self.assertTrue(os.path.exists(extensions_config_json))
901     with open(extensions_config_json, 'r') as content_file:
902       content = content_file.read()
903       js_file_name = 'xwalk-extensions/myextension/myextension.js'
904       self.assertTrue(content.find(js_file_name))
905       self.assertTrue(content.find('com.example.extension.MyExtension'))
906     extension_js = 'Example/assets/xwalk-extensions/myextension/myextension.js'
907     self.assertTrue(os.path.exists(extension_js))
908     extension_jar = 'Example/xwalk-extensions/myextension/myextension.jar'
909     self.assertTrue(os.path.exists(extension_jar))
910
911   def testEmptyMode(self):
912     self.executeCommandAndVerifyResult('make_apk.py')
913
914   def testCustomizeFile(self):
915     cmd = ['python', 'make_apk.py',
916            '--app-url=http://www.intel.com',
917            '--app-version=1.0.0',
918            '--name=Example',
919            '--package=org.xwalk.example',
920            '--verbose',
921            '--project-dir=.']
922     RunCommand(cmd)
923     manifest = 'Example/AndroidManifest.xml'
924     if not os.path.exists(manifest):
925       print 'The \'%s\' was not generated, please check it.' % manifest
926       sys.exit(1)
927
928     self.executeCommandAndVerifyResult('customize.py')
929
930   def testLaunchScreen(self):
931     # Prepare launch screen resources.
932     launch_screen_path = os.path.join('test_data', 'launchScreen')
933     orientations = ['default', 'portrait', 'landscape']
934     dimensions = ['0_75', '1', '1_5', '2']
935     img_types = ['img', 'bg']
936     for orientation in orientations:
937       for dimension in dimensions:
938         for img_type in img_types:
939           name = orientation + '_' + img_type + '_' + dimension
940           path_tmp = os.path.join(launch_screen_path, name)
941           _file = open(path_tmp, 'w+')
942           _file.write(name)
943           _file.close()
944     # Run Test.
945     manifest_path = os.path.join('test_data', 'launchScreen',
946                                  'manifest_deprecated_launch_screen.json')
947     cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
948            '--manifest=%s' % manifest_path, self._mode]
949     out = RunCommand(cmd)
950     self.assertIn('Warning: The following fields have been deprecated', out)
951     self.assertTrue(out.find('launch_screen') != -1)
952     Clean('Example', '1.0.0')
953     manifest_path = os.path.join('test_data', 'launchScreen', 'manifest.json')
954     cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
955            '--manifest=%s' % manifest_path, '--project-dir=.', self._mode]
956     RunCommand(cmd)
957     # Check theme.xml.
958     theme_path = os.path.join('Example', 'res', 'values-v14', 'theme.xml')
959     self.assertTrue(os.path.exists(theme_path))
960     with open(theme_path, 'r') as content_file:
961       content = content_file.read()
962     self.assertTrue(content.find('@drawable/launchscreen_bg') != -1)
963     # Check launchscreen_bg.xml
964     launch_screen_bg_path = os.path.join(
965         "Example", 'res', 'drawable', 'launchscreen_bg.xml')
966     self.assertTrue(os.path.exists(launch_screen_bg_path))
967     with open(launch_screen_bg_path, 'r') as content_file:
968       content = content_file.read()
969     self.assertTrue(content.find('@drawable/launchscreen_bg_img') != -1)
970     # Check resource images
971     for orientation in orientations:
972       for dimension in dimensions:
973         drawable = 'drawable'
974         if orientation == 'portrait':
975           drawable = drawable + '-port'
976         elif orientation == 'landscape':
977           drawable = drawable + '-land'
978         if dimension == '0_75':
979           drawable = drawable + '-ldpi'
980         elif dimension == '1':
981           drawable = drawable + '-mdpi'
982         elif dimension == '1_5':
983           drawable = drawable + '-hdpi'
984         elif dimension == '2':
985           drawable = drawable + '-xhdpi'
986         # Check background image
987         bg_drawable = os.path.join(
988             "Example", 'res', drawable, 'launchscreen_bg_img')
989         self.assertTrue(os.path.exists(bg_drawable))
990         with open(bg_drawable, 'r') as content_file:
991           content = content_file.read()
992         name = orientation + '_' + 'bg' + '_' + dimension
993         self.assertTrue(content == name)
994         # Check foreground image
995         fg_drawable = os.path.join(
996             "Example", 'res', drawable, 'launchscreen_img')
997         self.assertTrue(os.path.exists(fg_drawable))
998         with open(fg_drawable, 'r') as content_file:
999           content = content_file.read()
1000         name = orientation + '_' + 'img' + '_' + dimension
1001         self.assertTrue(content == name)
1002     self.checkApks('Example', '1.0.0')
1003     Clean('Example', '1.0.0')
1004
1005   def testTargetDir(self):
1006     test_option = ['./', '../', '~/']
1007     for option in test_option:
1008       cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
1009              '--package=org.xwalk.example', '--app-url=http://www.intel.com',
1010              '--target-dir=%s' % option, self._mode]
1011       RunCommand(cmd)
1012       self.addCleanup(Clean, os.path.expanduser('%sExample' % option), '1.0.0')
1013       if self._mode.find('shared') != -1:
1014         apk_path = os.path.expanduser('%sExample_1.0.0.apk' % option)
1015         self.assertTrue(os.path.exists(apk_path))
1016         self.checkApk(apk_path, '')
1017       elif self._mode.find('embedded') != -1:
1018         for arch in self.archs():
1019           apk_path = os.path.expanduser('%sExample_1.0.0_%s.apk'
1020                                         % (option, arch))
1021           self.assertTrue(os.path.exists(apk_path))
1022           self.checkApk(apk_path, arch)
1023
1024   def testCompressor(self):
1025     app_root = os.path.join('test_data', 'compressor')
1026     css_folder = os.path.join('test_data', 'compressor', 'css')
1027     css_file = os.path.join(css_folder, 'test.css')
1028     js_folder = os.path.join('test_data', 'compressor', 'js')
1029     js_file = os.path.join(js_folder, 'test.js')
1030     fun = self.assertTrue
1031     name = 'Example'
1032
1033     cmd = ['python', 'customize.py',
1034            '--name=%s' % name,
1035            '--package=org.xwalk.example',
1036            '--compressor',
1037            '--app-root=%s' % app_root,
1038            '--project-dir=.']
1039     RunCommand(cmd)
1040     CompareSizeForCompressor('all', css_file, 'css', name, fun)
1041     CompareSizeForCompressor('all', js_file, 'js', name, fun)
1042
1043     cmd = ['python', 'customize.py',
1044            '--name=%s' % name,
1045            '--package=org.xwalk.example',
1046            '--app-root=%s' % app_root,
1047            '--compressor',
1048            '--project-dir=.']
1049     RunCommand(cmd)
1050     CompareSizeForCompressor('all', css_file, 'css', name, fun)
1051     CompareSizeForCompressor('all', js_file, 'js', name, fun)
1052
1053     cmd = ['python', 'customize.py',
1054            '--name=%s' % name,
1055            '--package=org.xwalk.example',
1056            '--compressor=js',
1057            '--app-root=%s' % app_root,
1058            '--project-dir=.']
1059     RunCommand(cmd)
1060     CompareSizeForCompressor('js', js_file, 'js', name, fun)
1061
1062     cmd = ['python', 'customize.py',
1063            '--name=%s' % name,
1064            '--package=org.xwalk.example',
1065            '--compressor=css',
1066            '--app-root=%s' % app_root,
1067            '--project-dir=.']
1068     RunCommand(cmd)
1069     CompareSizeForCompressor('css', css_file, 'css', name, fun)
1070
1071     cmd = ['python', 'customize.py',
1072            '--name=%s' % name,
1073            '--package=org.xwalk.example',
1074            '--app-root=%s' % app_root,
1075            '--project-dir=.']
1076     RunCommand(cmd)
1077     CompareSizeForCompressor(None, css_file, 'css', name, fun)
1078     CompareSizeForCompressor(None, js_file, 'js', name, fun)
1079
1080     cmd = ['python', 'customize.py',
1081            '--name=%s' % name,
1082            '--package=org.xwalk.example',
1083            '--app-root=%s' % app_root,
1084            '--compressor=other',
1085            '--project-dir=.']
1086     RunCommand(cmd)
1087     CompareSizeForCompressor(None, css_file, 'css', name, fun)
1088     CompareSizeForCompressor(None, js_file, 'js', name, fun)
1089
1090     Clean(name, '1.0.0')
1091
1092
1093   def VerifyResultInXMLFile(self, xml_path, piece_content):
1094     self.assertTrue(os.path.exists(xml_path))
1095     with open(xml_path, 'r') as content_file:
1096       content = content_file.read()
1097
1098     self.assertIn(piece_content, content)
1099     Clean('Example', '1.0.0')
1100
1101
1102   def testAppNameWithNonASCII(self):
1103     xml_path = 'Example/AndroidManifest.xml'
1104     piece_content = 'android:label="%s"' % '你好'
1105     cmd = ['python', 'make_apk.py', '--name=你好', '--app-version=1.0.0',
1106            '--package=org.xwalk.example', '--app-url=http://www.intel.com',
1107            '--project-dir=.']
1108     RunCommand(cmd)
1109     self.VerifyResultInXMLFile(xml_path, piece_content)
1110
1111     manifest_path = os.path.join('test_data', 'manifest', 'invalidchars',
1112                                  'manifest_with_chinese_name.json')
1113     cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
1114            '--manifest=%s' % manifest_path, '--project-dir=.']
1115     RunCommand(cmd)
1116     self.VerifyResultInXMLFile(xml_path, piece_content)
1117
1118
1119   def testDescriptionWithDBCS(self):
1120     xml_path = 'Example/res/values/strings.xml'
1121     piece_content = '<string name="description">%s</string>' % '你好'
1122     cmd = ['python', 'make_apk.py', '--name=hello', '--app-version=1.0.0',
1123            '--package=org.xwalk.example', '--app-url=http://www.intel.com',
1124            '--description=你好', '--project-dir=.']
1125     RunCommand(cmd)
1126     self.VerifyResultInXMLFile(xml_path, piece_content)
1127
1128     manifest_path = os.path.join('test_data', 'manifest',
1129                                  'manifest_description_dbcs.json')
1130     cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
1131            '--manifest=%s' % manifest_path, '--project-dir=.']
1132     RunCommand(cmd)
1133     piece_content = '"description">%s</string>' % '你好 a sample description'
1134     self.VerifyResultInXMLFile(xml_path, piece_content)
1135
1136
1137 def SuiteWithModeOption():
1138   # Gather all the tests for the specified mode option.
1139   test_suite = unittest.TestSuite()
1140   test_suite.addTest(TestMakeApk('testAppBigVersionCodeBase'))
1141   test_suite.addTest(TestMakeApk('testAppVersionCode'))
1142   test_suite.addTest(TestMakeApk('testAppVersionCodeBase'))
1143   test_suite.addTest(TestMakeApk('testAppDescriptionAndVersion'))
1144   test_suite.addTest(TestMakeApk('testArch'))
1145   test_suite.addTest(TestMakeApk('testEnableRemoteDebugging'))
1146   test_suite.addTest(TestMakeApk('testEntry'))
1147   test_suite.addTest(TestMakeApk('testEntryWithErrors'))
1148   test_suite.addTest(TestMakeApk('testExtensionsWithOneExtension'))
1149   test_suite.addTest(TestMakeApk('testExtensionsWithNonExtension'))
1150   test_suite.addTest(TestMakeApk('testExtensionWithPermissions'))
1151   test_suite.addTest(TestMakeApk('testFullscreen'))
1152   test_suite.addTest(TestMakeApk('testIconByOption'))
1153   test_suite.addTest(TestMakeApk('testIconByManifest'))
1154   test_suite.addTest(TestMakeApk('testKeystore'))
1155   test_suite.addTest(TestMakeApk('testManifest'))
1156   test_suite.addTest(TestMakeApk('testManifestWithDeprecatedField'))
1157   test_suite.addTest(TestMakeApk('testManifestWithError'))
1158   test_suite.addTest(TestMakeApk('testName'))
1159   test_suite.addTest(TestMakeApk('testOrientation'))
1160   test_suite.addTest(TestMakeApk('testPackage'))
1161   test_suite.addTest(TestMakeApk('testPackageWithInvalidCharacter'))
1162   test_suite.addTest(TestMakeApk('testPermissions'))
1163   test_suite.addTest(TestMakeApk('testPermissionsWithError'))
1164   test_suite.addTest(TestMakeApk('testXPK'))
1165   test_suite.addTest(TestMakeApk('testXPKWithError'))
1166   test_suite.addTest(TestMakeApk('testTargetDir'))
1167   test_suite.addTest(TestMakeApk('testLaunchScreen'))
1168   return test_suite
1169
1170
1171 def SuiteWithEmptyModeOption():
1172   # Gather all the tests for empty mode option.
1173   test_suite = unittest.TestSuite()
1174   test_suite.addTest(TestMakeApk('testAppNameWithNonASCII'))
1175   test_suite.addTest(TestMakeApk('testCompressor'))
1176   test_suite.addTest(TestMakeApk('testCustomizeFile'))
1177   test_suite.addTest(TestMakeApk('testDescriptionWithDBCS'))
1178   test_suite.addTest(TestMakeApk('testEmptyMode'))
1179   test_suite.addTest(TestMakeApk('testToolVersion'))
1180   test_suite.addTest(TestMakeApk('testVerbose'))
1181   return test_suite
1182
1183
1184 def TestSuiteRun(test_runner, suite):
1185   results = test_runner.run(suite)
1186   return results.wasSuccessful()
1187
1188
1189 if __name__ == '__main__':
1190   parser = optparse.OptionParser()
1191   info = ('The build directory for xwalk.'
1192           'Such as: --build-dir=src/out')
1193   parser.add_option('--build-dir', help=info)
1194   info = ('The build target for xwalk.'
1195           'Such as: --target=Release')
1196   parser.add_option('--target', help=info)
1197   info = ('The path of package tool.')
1198   parser.add_option('--tool-path', help=info)
1199   info = ('The packaging mode for xwalk. Such as: --mode=embedded.'
1200           'Please refer the detail to the option of make_apk.py.')
1201   parser.add_option('--mode', help=info)
1202   options, dummy = parser.parse_args()
1203   if len(sys.argv) == 1:
1204     parser.print_help()
1205     sys.exit(1)
1206
1207   del sys.argv[1:]
1208   mode_suite = SuiteWithModeOption()
1209   empty_mode_suite = SuiteWithEmptyModeOption()
1210   runner = unittest.TextTestRunner(verbosity=2)
1211   if options.build_dir or options.target:
1212     warnings.warn(('"--build-dir" and "--target" will be deprecated soon, '
1213                    'please leverage "--tool-path" instead.'),
1214                   Warning)
1215   test_result = True
1216   if options.mode:
1217     test_result = TestSuiteRun(runner, mode_suite)
1218   else:
1219     # Run tests in both embedded and shared mode
1220     # when the mode option isn't specified.
1221     options.mode = 'embedded'
1222     print 'Run tests in embedded mode.'
1223     test_result = TestSuiteRun(runner, mode_suite)
1224     options.mode = 'shared'
1225     print 'Run tests in shared mode.'
1226     test_result = TestSuiteRun(runner, mode_suite) and test_result
1227     options.mode = ''
1228     print 'Run test without \'--mode\' option.'
1229     test_result = TestSuiteRun(runner, empty_mode_suite) and test_result
1230   if not test_result:
1231     sys.exit(1)