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