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