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.
17 def Clean(name, app_version):
18 if os.path.exists(name):
20 if options.mode == 'shared':
21 if os.path.isfile(name + '_' + app_version + '.apk'):
22 os.remove(name + '_' + app_version + '.apk')
24 if os.path.isfile(name + '_' + app_version + '_x86.apk'):
25 os.remove(name + '_' + app_version + '_x86.apk')
26 if os.path.isfile(name + '_' + app_version + '_arm.apk'):
27 os.remove(name + '_' + app_version + '_arm.apk')
30 def CompareSizeForCompressor(mode, original, ext, name, fun):
33 mode_list = ['all', 'js', 'css']
35 www_dir = os.path.join(name, 'assets', 'www')
36 if os.path.exists(www_dir):
37 size = GetFileSize(original)
38 compressed_file = os.path.join(www_dir, ext, 'test.' + ext)
39 compressed_size = GetFileSize(compressed_file)
42 fun(compressed_size < size)
44 fun(size == compressed_size)
46 print('Error: %s is not exist.' % www_dir)
49 def GetFileSize(file_path):
51 if os.path.exists(file_path):
52 size = os.path.getsize(file_path)
56 def RunCommand(command):
57 """Runs the command list, return the output."""
58 proc = subprocess.Popen(command, stdout=subprocess.PIPE,
59 stderr=subprocess.STDOUT, shell=False)
60 return proc.communicate()[0]
63 def GetResultWithOption(mode=None, manifest=None, name=None, package=None):
65 if manifest is not None:
66 manifest = '--manifest=' + manifest
68 app_url = '--app-url=http://www.intel.com'
70 name = '--name=' + name
71 if package is not None:
72 package = '--package=' + package
74 cmd = ['python', 'make_apk.py',
75 '--app-version=1.0.0',
81 return RunCommand(cmd)
84 class TestMakeApk(unittest.TestCase):
87 cls._original_dir = os.getcwd()
89 target_dir = os.path.expanduser(options.tool_path)
90 elif options.build_dir and options.target:
91 target_dir = os.path.join(options.build_dir,
94 if os.path.exists(target_dir):
95 # Prepare the test data.
96 test_src_dir = os.path.join(os.path.dirname(os.path.realpath(__file__)),
98 test_des_dir = os.path.join(target_dir, 'test_data')
99 if not os.path.exists(test_des_dir):
100 shutil.copytree(test_src_dir, test_des_dir)
103 unittest.SkipTest('xwalk_app_template folder doesn\'t exist. '
104 'Skipping all tests in make_apk_test.py')
106 if options.mode == 'shared':
107 cls._mode = '--mode=shared'
108 elif options.mode == 'embedded':
109 cls._mode = '--mode=embedded'
110 cls.fakeNativeLibrary()
113 def tearDownClass(cls):
114 # Clean the test data.
115 if options.tool_path:
116 test_data_dir = os.path.join(os.path.expanduser(options.tool_path),
118 elif options.build_dir and options.target:
119 test_data_dir = os.path.join(options.build_dir,
121 'xwalk_app_template',
123 if os.path.exists(test_data_dir):
124 shutil.rmtree(test_data_dir)
125 cls.restoreNativeLibrary()
126 os.chdir(cls._original_dir)
129 def fakeNativeLibrary():
130 # To reduce the time consumption of make_apk test for embedded mode,
131 # replace the original native library with an empty library.
132 # Because it doesn't affect the result of test.
133 if options.mode == 'embedded':
134 native_library_dir = 'native_libs'
135 native_library_temp_dir = 'temp'
136 shutil.copytree(native_library_dir, native_library_temp_dir)
137 for root, _, files in os.walk(native_library_dir):
138 if 'libxwalkcore.so' in files:
139 native_library_path = os.path.join(root, 'libxwalkcore.so')
140 # Remove the original library
141 os.remove(native_library_path)
142 # Create an empty library file
143 open(native_library_path, 'a').close()
146 def restoreNativeLibrary():
147 # Restore the original native library for embedded mode.
148 if options.mode == 'embedded':
149 native_library_dir = 'native_libs'
150 native_library_temp_dir = 'temp'
151 if os.path.exists(native_library_dir):
152 shutil.rmtree(native_library_dir)
153 shutil.move(native_library_temp_dir, native_library_dir)
157 x86_native_lib_path = os.path.join('native_libs', 'x86', 'libs',
158 'x86', 'libxwalkcore.so')
159 arm_native_lib_path = os.path.join('native_libs', 'armeabi-v7a', 'libs',
160 'armeabi-v7a', 'libxwalkcore.so')
162 if os.path.isfile(x86_native_lib_path):
163 arch_list.append('x86')
164 if os.path.isfile(arm_native_lib_path):
165 arch_list.append('arm')
168 def checkApks(self, apk_name, app_version):
169 # Check whether some files are contained in the given APK.
170 if self._mode.find('shared') != -1:
171 apk_path = '%s_%s.apk' % (apk_name, app_version)
172 self.checkApk(apk_path, '')
173 elif self._mode.find('embedded') != -1:
174 x86_apk_path = '%s_%s_x86.apk' % (apk_name, app_version)
175 if os.path.exists(x86_apk_path):
176 self.checkApk(x86_apk_path, 'x86')
177 arm_apk_path = '%s_%s_arm.apk' % (apk_name, app_version)
178 if os.path.exists(arm_apk_path):
179 self.checkApk(arm_apk_path, 'arm')
181 def checkApk(self, apk_path, arch):
182 # Check whether some files are contained in the given apk
183 # for specified arch.
184 cmd = ['jar', 'tvf', apk_path]
185 out = RunCommand(cmd)
186 common_files = ['AndroidManifest.xml', 'classes.dex']
187 for res_file in common_files:
188 self.assertTrue(out.find(res_file) != -1)
189 if self._mode.find('embedded') != -1:
190 embedded_related_files = ['icudtl.dat',
192 'device_capabilities_api.js',
193 'launch_screen_api.js',
194 'presentation_api.js']
195 for res_file in embedded_related_files:
196 self.assertTrue(out.find(res_file) != -1)
198 self.assertTrue(out.find('x86/libxwalkcore.so') != -1)
200 self.assertTrue(out.find('armeabi-v7a/libxwalkcore.so') != -1)
203 cmd = ['python', 'make_apk.py', '--app-version=1.0.0',
204 '--app-url=http://www.intel.com',
205 '--package=org.xwalk.example', self._mode]
206 out = RunCommand(cmd)
207 Clean('Example', '1.0.0')
208 self.assertIn('An APK name is required', out)
210 cmd = ['python', 'make_apk.py', '--name=Test_Example',
211 '--app-version=1.0.0', '--app-url=http://www.intel.com',
212 '--package=org.xwalk.example', self._mode]
213 out = RunCommand(cmd)
214 self.assertNotIn('An APK name is required', out)
215 Clean('Example', '1.0.0')
217 def testToolVersion(self):
218 cmd = ['python', 'make_apk.py', '--version']
219 out = RunCommand(cmd)
220 self.assertTrue(out.find('Crosswalk app packaging tool version') != -1)
222 def testAppDescriptionAndVersion(self):
223 cmd = ['python', 'make_apk.py', '--name=Example',
224 '--package=org.xwalk.example', '--app-version=1.0.0',
225 '--description=a sample application',
226 '--app-url=http://www.intel.com', self._mode]
228 self.addCleanup(Clean, 'Example', '1.0.0')
229 manifest = 'Example/AndroidManifest.xml'
230 with open(manifest, 'r') as content_file:
231 content = content_file.read()
232 self.assertTrue(os.path.exists(manifest))
233 self.assertTrue(content.find('description') != -1)
234 self.assertTrue(content.find('versionName') != -1)
235 self.checkApks('Example', '1.0.0')
237 def testAppVersionCode(self):
238 cmd = ['python', 'make_apk.py', '--name=Example',
239 '--package=org.xwalk.example', '--app-version=1.0.0',
240 '--description=a sample application',
241 '--app-versionCode=3',
242 '--app-url=http://www.intel.com', self._mode]
244 self.addCleanup(Clean, 'Example', '1.0.0')
245 manifest = 'Example/AndroidManifest.xml'
246 with open(manifest, 'r') as content_file:
247 content = content_file.read()
248 self.assertTrue(os.path.exists(manifest))
249 self.assertTrue(content.find('versionCode="3"') != -1)
250 self.checkApks('Example', '1.0.0')
252 def testAppVersionCodeBase(self):
253 # Arch option only works for embedded mode,
254 # so only test it for embedded mode.
255 if self._mode.find('embedded') == -1:
257 if 'x86' in self.archs():
259 versionCode = 'versionCode="60000003"'
262 versionCode = 'versionCode="20000003"'
263 cmd = ['python', 'make_apk.py', '--name=Example',
264 '--package=org.xwalk.example', '--app-version=1.0.0',
265 '--description=a sample application',
266 '--app-versionCodeBase=3',
268 '--app-url=http://www.intel.com', self._mode]
270 self.addCleanup(Clean, 'Example', '1.0.0')
271 manifest = 'Example/AndroidManifest.xml'
272 with open(manifest, 'r') as content_file:
273 content = content_file.read()
274 self.assertTrue(os.path.exists(manifest))
275 self.assertTrue(content.find(versionCode) != -1)
276 self.checkApks('Example', '1.0.0')
278 def testAppBigVersionCodeBase(self):
279 # Arch option only works for embedded mode,
280 # so only test it for embedded mode.
281 if self._mode.find('embedded') == -1:
283 if 'x86' in self.archs():
287 cmd = ['python', 'make_apk.py', '--name=Example',
288 '--package=org.xwalk.example', '--app-version=1.0.0',
289 '--description=a sample application',
290 '--app-versionCodeBase=30000000',
292 '--app-url=http://www.intel.com', self._mode]
294 self.addCleanup(Clean, 'Example', '1.0.0')
295 manifest = 'Example/AndroidManifest.xml'
296 self.assertFalse(os.path.exists(manifest))
298 def testPermissions(self):
299 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
300 '--package=org.xwalk.example', '--permissions=geolocation',
301 '--app-url=http://www.intel.com', self._mode]
303 self.addCleanup(Clean, 'Example', '1.0.0')
304 manifest = 'Example/AndroidManifest.xml'
305 with open(manifest, 'r') as content_file:
306 content = content_file.read()
307 self.assertTrue(os.path.exists(manifest))
308 self.assertTrue(content.find('ACCESS_FINE_LOCATION') != -1)
309 self.checkApks('Example', '1.0.0')
311 def testPermissionsWithError(self):
312 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
313 '--package=org.xwalk.example', '--permissions=UndefinedPermission',
314 '--app-url=http://www.intel.com', self._mode]
315 out = RunCommand(cmd)
316 self.assertTrue(out.find('related API is not supported.') != -1)
317 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
318 '--package=org.xwalk.example',
319 '--permissions=Contacts.Geolocation.Messaging',
320 '--app-url=http://www.intel.com', self._mode]
321 out = RunCommand(cmd)
322 self.assertTrue(out.find('related API is not supported.') != -1)
324 def testPackage(self):
325 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
326 '--app-url=http://www.intel.com', self._mode]
327 out = RunCommand(cmd)
328 self.assertIn('A package name is required', out)
330 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
331 '--package=org.xwalk.example',
332 '--app-url=http://www.intel.com', self._mode]
333 out = RunCommand(cmd)
334 self.assertNotIn('A package name is required', out)
335 Clean('Example', '1.0.0')
337 def testPackageWithInvalidCharacter(self):
338 package_name_error = 'package name should be started with letters'
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.assertIn(package_name_error, out)
346 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
347 '--package=org.xwalk.123example',
348 '--app-url=http://www.intel.com', self._mode]
349 out = RunCommand(cmd)
350 self.assertIn(package_name_error, out)
352 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
353 '--package=org.xwalk.example_',
354 '--app-url=http://www.intel.com', self._mode]
355 out = RunCommand(cmd)
356 self.assertNotIn(package_name_error, out)
357 Clean('Example', '1.0.0')
360 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
361 '--package=org.xwalk.example', '--app-url=http://www.intel.com',
363 out = RunCommand(cmd)
364 self.assertNotIn('You must pass either "--app-url" or',
366 self.assertNotIn('You must specify both "--app-local-path" and',
368 self.addCleanup(Clean, 'Example', '1.0.0')
369 self.checkApks('Example', '1.0.0')
370 Clean('Example', '1.0.0')
372 test_entry_root = 'test_data/entry'
373 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
374 '--package=org.xwalk.example', '--app-root=%s' % test_entry_root,
375 '--app-local-path=index.html', self._mode]
376 out = RunCommand(cmd)
377 self.assertNotIn('You must pass either "--app-url" or',
379 self.assertNotIn('You must specify both "--app-local-path" and',
381 self.checkApks('Example', '1.0.0')
383 def testEntryWithErrors(self):
384 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
385 '--package=org.xwalk.example', self._mode]
386 out = RunCommand(cmd)
387 self.addCleanup(Clean, 'Example', '1.0.0')
388 self.assertIn('You must pass either "--app-url" or',
391 self.assertFalse(os.path.exists('Example.apk'))
392 Clean('Example', '1.0.0')
394 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
395 '--package=org.xwalk.example', '--app-url=http://www.intel.com',
396 '--app-root=.', self._mode]
397 out = RunCommand(cmd)
398 self.assertIn('You must pass either "--app-url" or',
400 self.assertFalse(os.path.exists('Example.apk'))
401 Clean('Example', '1.0.0')
403 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
404 '--package=org.xwalk.example', '--app-root=./', self._mode]
405 out = RunCommand(cmd)
406 self.assertIn('You must specify both "--app-local-path" and',
408 self.assertFalse(os.path.exists('Example.apk'))
409 Clean('Example', '1.0.0')
411 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
412 '--package=org.xwalk.example', '--app-local-path=index.html',
414 out = RunCommand(cmd)
415 self.assertIn('You must specify both "--app-local-path" and',
417 self.assertFalse(os.path.exists('Example.apk'))
418 Clean('Example', '1.0.0')
420 manifest_path = os.path.join('test_data', 'manifest',
421 'manifest_app_launch_local_path.json')
422 cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
423 '--manifest=%s' % manifest_path, self._mode]
424 out = RunCommand(cmd)
426 out.find('Please make sure that the local path file') != -1)
427 self.assertFalse(os.path.exists('Example.apk'))
429 def testIconByOption(self):
430 icon = os.path.join('test_data', 'manifest', 'icons', 'icon_96.png')
431 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
432 '--package=org.xwalk.example', '--app-url=http://www.intel.com',
433 '--icon=%s' % icon, self._mode]
435 self.addCleanup(Clean, 'Example', '1.0.0')
436 manifest = 'Example/AndroidManifest.xml'
437 with open(manifest, 'r') as content_file:
438 content = content_file.read()
439 self.assertTrue(content.find('drawable/icon_96') != -1)
440 self.checkApks('Example', '1.0.0')
442 def testIconByManifest(self):
443 manifest_path = os.path.join('test_data', 'manifest', 'manifest_icon.json')
444 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
445 '--package=org.xwalk.example', '--app-url=http://www.intel.com',
446 '--manifest=%s' % manifest_path, self._mode]
448 self.addCleanup(Clean, 'Example', '1.0.0')
449 manifest = 'Example/AndroidManifest.xml'
450 with open(manifest, 'r') as content_file:
451 content = content_file.read()
452 self.assertTrue(content.find('drawable/icon') != -1)
453 self.checkApks('Example', '1.0.0')
455 def testFullscreen(self):
456 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
457 '--package=org.xwalk.example', '--app-url=http://www.intel.com',
460 self.addCleanup(Clean, 'Example', '1.0.0')
461 theme = 'Example/res/values-v17/theme.xml'
462 with open(theme, 'r') as content_file:
463 content = content_file.read()
464 self.assertTrue(os.path.exists(theme))
467 '<item name="android:windowFullscreen">true</item>') != -1)
468 self.checkApks('Example', '1.0.0')
470 def testEnableRemoteDebugging(self):
471 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
472 '--package=org.xwalk.example', '--app-url=http://www.intel.com',
473 '--enable-remote-debugging', self._mode]
475 self.addCleanup(Clean, 'Example', '1.0.0')
476 activity = 'Example/src/org/xwalk/example/ExampleActivity.java'
477 with open(activity, 'r') as content_file:
478 content = content_file.read()
479 self.assertTrue(os.path.exists(activity))
480 self.assertTrue(content.find('setRemoteDebugging') != -1)
481 self.checkApks('Example', '1.0.0')
482 Clean('Example', '1.0.0')
483 manifest_path = os.path.join('test_data', 'manifest', 'manifest.json')
484 cmd = ['python', 'make_apk.py', '--enable-remote-debugging',
485 '--package=org.xwalk.example',
486 '--manifest=%s' % manifest_path, self._mode]
488 activity = 'Example/src/org/xwalk/example/ExampleActivity.java'
489 with open(activity, 'r') as content_file:
490 content = content_file.read()
491 self.assertTrue(os.path.exists(activity))
492 self.assertTrue(content.find('setRemoteDebugging') != -1)
493 self.checkApks('Example', '1.0.0')
495 def testKeystore(self):
496 keystore_path = os.path.join('test_data', 'keystore',
497 'xwalk-test.keystore')
498 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
499 '--package=org.xwalk.example', '--app-url=http://www.intel.com',
500 '--keystore-path=%s' % keystore_path, '--keystore-alias=xwalk-test',
501 '--keystore-passcode=xwalk-test', self._mode]
503 self.addCleanup(Clean, 'Example', '1.0.0')
504 self.assertTrue(os.path.exists('Example'))
505 apk_list = ['Example.apk', 'Example_x86.apk', 'Example_arm.apk']
507 if os.path.isfile(apk):
508 cmd = ['jarsigner', '-verify', '-keystore',
509 keystore_path, '-verbose', apk]
510 out = RunCommand(cmd)
511 self.assertTrue(out.find('smk') != -1)
512 self.checkApks('Example', '1.0.0')
514 def testManifest(self):
515 manifest_path = os.path.join('test_data', 'manifest', 'manifest.json')
516 cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
517 '--manifest=%s' % manifest_path, self._mode]
519 self.addCleanup(Clean, 'Example', '1.0.0')
520 manifest = 'Example/AndroidManifest.xml'
521 with open(manifest, 'r') as content_file:
522 content = content_file.read()
523 self.assertTrue(os.path.exists(manifest))
524 self.assertTrue(content.find('android.permission.READ_CONTACTS') != -1)
525 self.assertTrue(content.find('android.permission.WRITE_CONTACTS') != -1)
527 content.find('android.permission.ACCESS_FINE_LOCATION') != -1)
528 self.assertTrue(content.find('android.permission.READ_SMS') != -1)
529 self.assertTrue(content.find('android.permission.RECEIVE_SMS') != -1)
530 self.assertTrue(content.find('android.permission.SEND_SMS') != -1)
531 self.assertTrue(content.find('android.permission.WRITE_SMS') != -1)
532 self.assertTrue(content.find('landscape') != -1)
533 theme = 'Example/res/values-v17/theme.xml'
534 with open(theme, 'r') as content_file:
535 content = content_file.read()
536 self.assertTrue(os.path.exists(theme))
539 '<item name="android:windowFullscreen">true</item>') != -1)
540 self.assertTrue(os.path.exists('Example'))
541 self.checkApks('Example', '1.0.0')
543 def testManifestWithSpecificValue(self):
544 manifest_path = os.path.join('test_data', 'manifest',
545 'manifest_app_launch_local_path.json')
546 cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
547 '--manifest=%s' % manifest_path, self._mode]
548 out = RunCommand(cmd)
549 self.addCleanup(Clean, 'Example', '1.0.0')
550 self.assertTrue(out.find('no app launch path') == -1)
551 self.checkApks('Example', '1.0.0')
553 def testManifestWithDeprecatedField(self):
554 manifest_path = os.path.join('test_data', 'manifest', 'deprecated',
555 'manifest_app_local_path.json')
556 cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
557 '--manifest=%s' % manifest_path, self._mode]
558 out = RunCommand(cmd)
559 self.addCleanup(Clean, 'Example', '1.0.0')
560 self.assertIn('WARNING: app.launch.local_path is deprecated for Crosswalk',
562 Clean('Example', '1.0.0')
564 manifest_path = os.path.join('test_data', 'manifest', 'deprecated',
565 'manifest_launch_path.json')
566 cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
567 '--manifest=%s' % manifest_path, self._mode]
568 out = RunCommand(cmd)
569 self.assertIn('WARNING: launch_path is deprecated for Crosswalk', out)
570 Clean('Example', '1.0.0')
572 manifest_path = os.path.join('test_data', 'manifest', 'deprecated',
573 'manifest_permissions.json')
574 cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
575 '--manifest=%s' % manifest_path, self._mode]
576 out = RunCommand(cmd)
577 self.assertIn('WARNING: permissions is deprecated for Crosswalk', out)
578 Clean('Example', '1.0.0')
580 manifest_path = os.path.join('test_data', 'manifest',
581 'manifest_deprecated_icon.json')
582 cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
583 '--manifest=%s' % manifest_path, self._mode]
584 out = RunCommand(cmd)
585 self.assertIn('WARNING: icons defined as dictionary form is deprecated',
587 Clean('Example', '1.0.0')
589 manifest_path = os.path.join('test_data', 'manifest', 'deprecated',
590 'manifest_description.json')
591 cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
592 '--manifest=%s' % manifest_path, self._mode]
593 out = RunCommand(cmd)
594 self.assertIn('WARNING: description is deprecated for Crosswalk', out)
595 Clean('Example', '1.0.0')
597 manifest_path = os.path.join('test_data', 'manifest', 'deprecated',
598 'manifest_deprecated_version.json')
599 cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
600 '--manifest=%s' % manifest_path, self._mode]
601 out = RunCommand(cmd)
602 self.assertIn('WARNING: version is deprecated for Crosswalk', out)
604 def testManifestWithError(self):
605 manifest_path = os.path.join('test_data', 'manifest',
606 'manifest_no_app_launch_path.json')
607 cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
608 '--manifest=%s' % manifest_path, '--verbose', self._mode]
609 out = RunCommand(cmd)
610 self.assertTrue(out.find('no app launch path') != -1)
611 manifest_path = os.path.join('test_data', 'manifest',
612 'manifest_no_name.json')
613 cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
614 '--manifest=%s' % manifest_path, '--verbose', self._mode]
615 out = RunCommand(cmd)
616 self.assertTrue(out.find('no \'name\' field') != -1)
617 manifest_path = os.path.join('test_data', 'manifest',
618 'manifest_permissions_format_error.json')
619 cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
620 '--manifest=%s' % manifest_path, '--verbose', self._mode]
621 out = RunCommand(cmd)
622 self.assertTrue(out.find('\'Permissions\' field error') != -1)
623 manifest_path = os.path.join('test_data', 'manifest',
624 'manifest_permissions_field_error.json')
625 cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
626 '--manifest=%s' % manifest_path, '--verbose', self._mode]
627 out = RunCommand(cmd)
628 self.assertTrue(out.find('\'Permissions\' field error') != -1)
629 manifest_path = os.path.join('test_data', 'manifest',
630 'manifest_not_supported_permission.json')
631 cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
632 '--manifest=%s' % manifest_path, '--verbose', self._mode]
633 out = RunCommand(cmd)
635 out.find('\'Telephony\' related API is not supported') != -1)
637 def testExtensionsWithOneExtension(self):
638 # Test with an existed extension.
639 extension_path = 'test_data/extensions/myextension'
640 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
641 '--package=org.xwalk.example', '--app-url=http://www.intel.com',
642 '--extensions=%s' % extension_path, self._mode]
644 self.addCleanup(Clean, 'Example', '1.0.0')
645 self.assertTrue(os.path.exists('Example'))
646 extensions_config_json = 'Example/assets/extensions-config.json'
647 self.assertTrue(os.path.exists(extensions_config_json))
648 with open(extensions_config_json, 'r') as content_file:
649 content = content_file.read()
651 content.find('xwalk-extensions/myextension/myextension.js'))
652 self.assertTrue(content.find('com.example.extension.MyExtension'))
653 extension_js = 'Example/assets/xwalk-extensions/myextension/myextension.js'
654 self.assertTrue(os.path.exists(extension_js))
655 extension_jar = 'Example/xwalk-extensions/myextension/myextension.jar'
656 self.assertTrue(os.path.exists(extension_jar))
657 self.checkApks('Example', '1.0.0')
659 def testExtensionsWithNonExtension(self):
660 # Test with a non-existed extension.
661 extension_path = 'test_data/extensions/myextension'
662 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
663 '--package=org.xwalk.example', '--app-url=http://www.intel.com',
664 '--extensions=%s1' % extension_path, self._mode, '--verbose']
665 out = RunCommand(cmd)
666 error_msg = 'Error: can\'t find the extension directory'
667 self.assertTrue(out.find(error_msg) != -1)
668 self.assertTrue(out.find('Exiting with error code: 9') != -1)
670 def testExtensionWithPermissions(self):
671 test_entry_root = 'test_data/entry'
672 # Add redundant separators for test.
673 extension_path = 'test_data//extensions/contactextension/'
674 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
675 '--package=org.xwalk.example', '--app-root=%s' % test_entry_root,
676 '--app-local-path=contactextension.html',
677 '--extensions=%s' % extension_path, self._mode]
679 self.addCleanup(Clean, 'Example', '1.0.0')
680 self.assertTrue(os.path.exists('Example'))
681 manifest = 'Example/AndroidManifest.xml'
682 with open(manifest, 'r') as content_file:
683 content = content_file.read()
684 self.assertTrue(os.path.exists(manifest))
685 self.assertTrue(content.find('android.permission.WRITE_CONTACTS') != -1)
686 self.assertTrue(content.find('android.permission.READ_CONTACTS') != -1)
687 self.checkApks('Example', '1.0.0')
690 xpk_file = os.path.join('test_data', 'xpk', 'example.xpk')
691 cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
692 '--xpk=%s' % xpk_file, self._mode]
694 self.addCleanup(Clean, 'Example', '1.0.0')
695 self.assertTrue(os.path.exists('Example'))
696 self.checkApks('Example', '1.0.0')
698 def testXPKWithError(self):
699 xpk_file = os.path.join('test_data', 'xpk', 'error.xpk')
700 cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
701 '--xpk=%s' % xpk_file, self._mode]
702 out = RunCommand(cmd)
703 error_msg = 'XPK doesn\'t contain manifest file'
704 self.assertTrue(out.find(error_msg) != -1)
705 self.assertFalse(os.path.exists('Example'))
707 def testOrientation(self):
708 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
709 '--package=org.xwalk.example', '--app-url=http://www.intel.com',
710 '--orientation=landscape', self._mode]
712 self.addCleanup(Clean, 'Example', '1.0.0')
713 manifest = 'Example/AndroidManifest.xml'
714 with open(manifest, 'r') as content_file:
715 content = content_file.read()
716 self.assertTrue(os.path.exists(manifest))
717 self.assertTrue(content.find('landscape') != -1)
718 self.assertTrue(os.path.exists('Example'))
719 self.checkApks('Example', '1.0.0')
722 # Arch option only works for embedded mode,
723 # so only test it for embedded mode.
724 if self._mode.find('embedded') != -1:
725 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
726 '--package=org.xwalk.example', '--app-url=http://www.intel.com',
727 '--arch=x86', self._mode]
729 self.addCleanup(Clean, 'Example', '1.0.0')
730 if 'x86' in self.archs():
731 self.assertTrue(os.path.isfile('Example_1.0.0_x86.apk'))
732 self.checkApk('Example_1.0.0_x86.apk', 'x86')
734 self.assertFalse(os.path.isfile('Example_1.0.0_x86.apk'))
735 self.assertFalse(os.path.isfile('Example_1.0.0_arm.apk'))
736 Clean('Example', '1.0.0')
737 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
738 '--package=org.xwalk.example', '--app-url=http://www.intel.com',
739 '--arch=arm', self._mode]
741 if 'arm' in self.archs():
742 self.assertTrue(os.path.isfile('Example_1.0.0_arm.apk'))
743 self.checkApk('Example_1.0.0_arm.apk', 'arm')
745 self.assertFalse(os.path.isfile('Example_1.0.0._arm.apk'))
746 self.assertFalse(os.path.isfile('Example_1.0.0_x86.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',
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')
756 self.assertFalse(os.path.isfile('Example_1.0.0._arm.apk'))
757 if 'x86' in self.archs():
758 self.assertTrue(os.path.isfile('Example_1.0.0_x86.apk'))
759 self.checkApk('Example_1.0.0_x86.apk', 'x86')
761 self.assertFalse(os.path.isfile('Example_1.0.0._x86.apk'))
762 Clean('Example', '1.0.0')
763 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
764 '--package=org.xwalk.example', '--app-url=http://www.intel.com',
765 '--arch=undefined', self._mode]
766 out = RunCommand(cmd)
767 error_msg = 'invalid choice: \'undefined\''
768 self.assertTrue(out.find(error_msg) != -1)
770 def testVerbose(self):
771 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
772 '--package=org.xwalk.example', '--app-url=http://www.intel.com',
773 '--verbose', self._mode]
774 result = RunCommand(cmd)
775 self.addCleanup(Clean, 'Example', '1.0.0')
776 self.assertTrue(result.find('aapt') != -1)
777 self.assertTrue(result.find('crunch') != -1)
778 self.assertTrue(result.find('apkbuilder') != -1)
779 self.assertTrue(os.path.exists('Example'))
780 self.checkApks('Example', '1.0.0')
782 def executeCommandAndVerifyResult(self, exec_file):
783 # Test all of supported options with empty 'mode' option.
784 icon_path = './app_src/res/drawable-xhdpi/crosswalk.png'
785 extension_path = 'test_data/extensions/myextension'
788 if exec_file.find("make_apk.py") != -1:
790 icon = '--icon=%s' % icon_path
791 cmd = ['python', '%s' % exec_file,
792 '--app-version=1.0.0',
793 '--app-url=http://www.intel.com',
795 '--description=a sample application',
796 '--enable-remote-debugging',
797 '--extensions=%s' % extension_path,
802 '--orientation=landscape',
803 '--package=org.xwalk.example',
804 '--permissions=geolocation']
806 self.addCleanup(Clean, 'Example', '1.0.0')
807 activity = 'Example/src/org/xwalk/example/ExampleActivity.java'
808 with open(activity, 'r') as content_file:
809 content = content_file.read()
810 self.assertTrue(os.path.exists(activity))
811 # Test remote debugging option.
812 self.assertTrue(content.find('setRemoteDebugging') != -1)
813 # Test keep screen on option
814 self.assertTrue(content.find('FLAG_KEEP_SCREEN_ON') != -1)
816 manifest = 'Example/AndroidManifest.xml'
817 with open(manifest, 'r') as content_file:
818 content = content_file.read()
819 self.assertTrue(os.path.exists(manifest))
820 # Test permission option.
821 self.assertTrue(content.find('ACCESS_FINE_LOCATION') != -1)
822 # Test description option.
823 self.assertTrue(content.find('description') != -1)
824 # Test app version option.
825 self.assertTrue(content.find('versionName') != -1)
826 # Test orientation option.
827 self.assertTrue(content.find('landscape') != -1)
828 # Test fullscreen option
829 theme = 'Example/res/values-v17/theme.xml'
830 with open(theme, 'r') as content_file:
831 content = content_file.read()
832 self.assertTrue(os.path.exists(theme))
835 '<item name="android:windowFullscreen">true</item>') != -1)
836 # Test extensions option.
837 extensions_config_json = 'Example/assets/extensions-config.json'
838 self.assertTrue(os.path.exists(extensions_config_json))
839 with open(extensions_config_json, 'r') as content_file:
840 content = content_file.read()
841 js_file_name = 'xwalk-extensions/myextension/myextension.js'
842 self.assertTrue(content.find(js_file_name))
843 self.assertTrue(content.find('com.example.extension.MyExtension'))
844 extension_js = 'Example/assets/xwalk-extensions/myextension/myextension.js'
845 self.assertTrue(os.path.exists(extension_js))
846 extension_jar = 'Example/xwalk-extensions/myextension/myextension.jar'
847 self.assertTrue(os.path.exists(extension_jar))
849 def testEmptyMode(self):
850 self.executeCommandAndVerifyResult('make_apk.py')
852 def testCustomizeFile(self):
853 cmd = ['python', 'make_apk.py',
854 '--app-url=http://www.intel.com',
855 '--app-version=1.0.0',
857 '--package=org.xwalk.example',
860 manifest = 'Example/AndroidManifest.xml'
861 if not os.path.exists(manifest):
862 print 'The \'%s\' was not generated, please check it.' % manifest
865 self.executeCommandAndVerifyResult('customize.py')
867 def testLaunchScreen(self):
868 # Prepare launch screen resources.
869 launch_screen_path = os.path.join('test_data', 'launchScreen')
870 orientations = ['default', 'portrait', 'landscape']
871 dimensions = ['0_75', '1', '1_5', '2']
872 img_types = ['img', 'bg']
873 for orientation in orientations:
874 for dimension in dimensions:
875 for img_type in img_types:
876 name = orientation + '_' + img_type + '_' + dimension
877 path_tmp = os.path.join(launch_screen_path, name)
878 _file = open(path_tmp,'w+')
882 manifest_path = os.path.join('test_data', 'launchScreen',
883 'manifest_deprecated_launch_screen.json')
884 cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
885 '--manifest=%s' % manifest_path, self._mode]
886 out = RunCommand(cmd)
888 out.find('WARNING: launch_screen is deprecated for Crosswalk') != -1)
889 Clean('Example', '1.0.0')
890 manifest_path = os.path.join('test_data', 'launchScreen', 'manifest.json')
891 cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
892 '--manifest=%s' % manifest_path, self._mode]
895 theme_path = os.path.join('Example', 'res', 'values-v17', 'theme.xml')
896 self.assertTrue(os.path.exists(theme_path))
897 with open(theme_path, 'r') as content_file:
898 content = content_file.read()
899 self.assertTrue(content.find('@drawable/launchscreen_bg') != -1)
900 # Check launchscreen_bg.xml
901 launch_screen_bg_path = os.path.join(
902 "Example", 'res', 'drawable', 'launchscreen_bg.xml')
903 self.assertTrue(os.path.exists(launch_screen_bg_path))
904 with open(launch_screen_bg_path, 'r') as content_file:
905 content = content_file.read()
906 self.assertTrue(content.find('@drawable/launchscreen_bg_img') != -1)
907 # Check resource images
908 for orientation in orientations:
909 for dimension in dimensions:
910 drawable = 'drawable'
911 if orientation == 'portrait':
912 drawable = drawable + '-port'
913 elif orientation == 'landscape':
914 drawable = drawable + '-land'
915 if dimension == '0_75':
916 drawable = drawable + '-ldpi'
917 elif dimension == '1':
918 drawable = drawable + '-mdpi'
919 elif dimension == '1_5':
920 drawable = drawable + '-hdpi'
921 elif dimension == '2':
922 drawable = drawable + '-xhdpi'
923 # Check background image
924 bg_drawable = os.path.join(
925 "Example", 'res', drawable, 'launchscreen_bg_img')
926 self.assertTrue(os.path.exists(bg_drawable))
927 with open(bg_drawable, 'r') as content_file:
928 content = content_file.read()
929 name = orientation + '_' + 'bg' + '_' + dimension
930 self.assertTrue(content == name)
931 # Check foreground image
932 fg_drawable = os.path.join(
933 "Example", 'res', drawable, 'launchscreen_img')
934 self.assertTrue(os.path.exists(fg_drawable))
935 with open(fg_drawable, 'r') as content_file:
936 content = content_file.read()
937 name = orientation + '_' + 'img' + '_' + dimension
938 self.assertTrue(content == name)
939 self.checkApks('Example', '1.0.0')
940 Clean('Example', '1.0.0')
942 def testTargetDir(self):
943 test_option = ['./', '../', '~/']
944 for option in test_option:
945 cmd = ['python', 'make_apk.py', '--name=Example', '--app-version=1.0.0',
946 '--package=org.xwalk.example', '--app-url=http://www.intel.com',
947 '--target-dir=%s' % option, self._mode]
949 self.addCleanup(Clean, os.path.expanduser('%sExample' % option), '1.0.0')
950 if self._mode.find('shared') != -1:
951 apk_path = os.path.expanduser('%sExample_1.0.0.apk' % option)
952 self.assertTrue(os.path.exists(apk_path))
953 self.checkApk(apk_path, '')
954 elif self._mode.find('embedded') != -1:
955 for arch in self.archs():
956 apk_path = os.path.expanduser('%sExample_1.0.0_%s.apk'
958 self.assertTrue(os.path.exists(apk_path))
959 self.checkApk(apk_path, arch)
961 def testCompressor(self):
962 app_root = os.path.join('test_data', 'compressor')
963 css_folder = os.path.join('test_data', 'compressor', 'css')
964 css_file = os.path.join(css_folder, 'test.css')
965 js_folder = os.path.join('test_data', 'compressor', 'js')
966 js_file = os.path.join(js_folder, 'test.js')
967 fun = self.assertTrue
970 cmd = ['python', 'customize.py',
972 '--package=org.xwalk.example',
974 '--app-root=%s' % app_root]
976 CompareSizeForCompressor('all', css_file, 'css', name, fun)
977 CompareSizeForCompressor('all', js_file, 'js', name, fun)
979 cmd = ['python', 'customize.py',
981 '--package=org.xwalk.example',
982 '--app-root=%s' % app_root,
985 CompareSizeForCompressor('all', css_file, 'css', name, fun)
986 CompareSizeForCompressor('all', js_file, 'js', name, fun)
988 cmd = ['python', 'customize.py',
990 '--package=org.xwalk.example',
992 '--app-root=%s' % app_root]
994 CompareSizeForCompressor('js', js_file, 'js', name, fun)
996 cmd = ['python', 'customize.py',
998 '--package=org.xwalk.example',
1000 '--app-root=%s' % app_root]
1002 CompareSizeForCompressor('css', css_file, 'css', name, fun)
1004 cmd = ['python', 'customize.py',
1006 '--package=org.xwalk.example',
1007 '--app-root=%s' % app_root]
1009 CompareSizeForCompressor(None, css_file, 'css', name, fun)
1010 CompareSizeForCompressor(None, js_file, 'js', name, fun)
1012 cmd = ['python', 'customize.py',
1014 '--package=org.xwalk.example',
1015 '--app-root=%s' % app_root,
1016 '--compressor=other']
1018 CompareSizeForCompressor(None, css_file, 'css', name, fun)
1019 CompareSizeForCompressor(None, js_file, 'js', name, fun)
1021 Clean(name, '1.0.0')
1024 def verifyResultForAppName(self, app_name):
1025 android_manifest = 'Example/AndroidManifest.xml'
1026 self.assertTrue(os.path.exists(android_manifest))
1027 with open(android_manifest, 'r') as content_file:
1028 content = content_file.read()
1029 label_name = 'android:label="%s"' % app_name
1030 self.assertIn(label_name, content)
1031 Clean('Example', '1.0.0')
1034 def testAppNameWithNonASCII (self):
1035 cmd = ['python', 'make_apk.py', '--name=你好', '--app-version=1.0.0',
1036 '--package=org.xwalk.example', '--app-url=http://www.intel.com']
1038 self.verifyResultForAppName('你好')
1040 manifest_path = os.path.join('test_data', 'manifest', 'invalidchars',
1041 'manifest_with_chinese_name.json')
1042 cmd = ['python', 'make_apk.py', '--package=org.xwalk.example',
1043 '--manifest=%s' % manifest_path]
1045 self.verifyResultForAppName('你好')
1048 def SuiteWithModeOption():
1049 # Gather all the tests for the specified mode option.
1050 test_suite = unittest.TestSuite()
1051 test_suite.addTest(TestMakeApk('testAppBigVersionCodeBase'))
1052 test_suite.addTest(TestMakeApk('testAppVersionCode'))
1053 test_suite.addTest(TestMakeApk('testAppVersionCodeBase'))
1054 test_suite.addTest(TestMakeApk('testAppDescriptionAndVersion'))
1055 test_suite.addTest(TestMakeApk('testArch'))
1056 test_suite.addTest(TestMakeApk('testEnableRemoteDebugging'))
1057 test_suite.addTest(TestMakeApk('testEntry'))
1058 test_suite.addTest(TestMakeApk('testEntryWithErrors'))
1059 test_suite.addTest(TestMakeApk('testExtensionsWithOneExtension'))
1060 test_suite.addTest(TestMakeApk('testExtensionsWithNonExtension'))
1061 test_suite.addTest(TestMakeApk('testExtensionWithPermissions'))
1062 test_suite.addTest(TestMakeApk('testFullscreen'))
1063 test_suite.addTest(TestMakeApk('testIconByOption'))
1064 test_suite.addTest(TestMakeApk('testIconByManifest'))
1065 test_suite.addTest(TestMakeApk('testKeystore'))
1066 test_suite.addTest(TestMakeApk('testManifest'))
1067 test_suite.addTest(TestMakeApk('testManifestWithDeprecatedField'))
1068 test_suite.addTest(TestMakeApk('testManifestWithError'))
1069 test_suite.addTest(TestMakeApk('testName'))
1070 test_suite.addTest(TestMakeApk('testOrientation'))
1071 test_suite.addTest(TestMakeApk('testPackage'))
1072 test_suite.addTest(TestMakeApk('testPackageWithInvalidCharacter'))
1073 test_suite.addTest(TestMakeApk('testPermissions'))
1074 test_suite.addTest(TestMakeApk('testPermissionsWithError'))
1075 test_suite.addTest(TestMakeApk('testXPK'))
1076 test_suite.addTest(TestMakeApk('testXPKWithError'))
1077 test_suite.addTest(TestMakeApk('testTargetDir'))
1078 test_suite.addTest(TestMakeApk('testLaunchScreen'))
1082 def SuiteWithEmptyModeOption():
1083 # Gather all the tests for empty mode option.
1084 test_suite = unittest.TestSuite()
1085 test_suite.addTest(TestMakeApk('testAppNameWithNonASCII'))
1086 test_suite.addTest(TestMakeApk('testCompressor'))
1087 test_suite.addTest(TestMakeApk('testCustomizeFile'))
1088 test_suite.addTest(TestMakeApk('testEmptyMode'))
1089 test_suite.addTest(TestMakeApk('testToolVersion'))
1090 test_suite.addTest(TestMakeApk('testVerbose'))
1094 def TestSuiteRun(test_runner, suite):
1095 results = test_runner.run(suite)
1096 return results.wasSuccessful()
1099 if __name__ == '__main__':
1100 parser = optparse.OptionParser()
1101 info = ('The build directory for xwalk.'
1102 'Such as: --build-dir=src/out')
1103 parser.add_option('--build-dir', help=info)
1104 info = ('The build target for xwalk.'
1105 'Such as: --target=Release')
1106 parser.add_option('--target', help=info)
1107 info = ('The path of package tool.')
1108 parser.add_option('--tool-path', help=info)
1109 info = ('The packaging mode for xwalk. Such as: --mode=embedded.'
1110 'Please refer the detail to the option of make_apk.py.')
1111 parser.add_option('--mode', help=info)
1112 options, dummy = parser.parse_args()
1113 if len(sys.argv) == 1:
1118 mode_suite = SuiteWithModeOption()
1119 empty_mode_suite = SuiteWithEmptyModeOption()
1120 runner = unittest.TextTestRunner(verbosity=2)
1121 if options.build_dir or options.target:
1122 warnings.warn(('"--build-dir" and "--target" will be deprecated soon, '
1123 'please leverage "--tool-path" instead.'),
1127 test_result = TestSuiteRun(runner, mode_suite)
1129 # Run tests in both embedded and shared mode
1130 # when the mode option isn't specified.
1131 options.mode = 'embedded'
1132 print 'Run tests in embedded mode.'
1133 test_result = TestSuiteRun(runner, mode_suite)
1134 options.mode = 'shared'
1135 print 'Run tests in shared mode.'
1136 test_result = TestSuiteRun(runner, mode_suite) and test_result
1138 print 'Run test without \'--mode\' option.'
1139 test_result = TestSuiteRun(runner, empty_mode_suite) and test_result