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