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