def test_run_android_apk(self): os.path.exists = lambda unused_filename: True __builtin__.open = FileMockFactory( self, '<manifest ' ' xmlns:android="http://schemas.android.com/apk/res/android"\n' ' package="com.google.fpl.libfplutil_test">\n' ' <uses-sdk android:minSdkVersion="1"/>\n' ' <application>\n' ' <activity android:name="android.app.NativeActivity">\n' ' <meta-data android:name="android.app.lib_name"\n' ' android:value="test"/>\n' ' </activity>\n' ' </application>\n' '</manifest>') build_environment = android.BuildEnvironment( android.BuildEnvironment.build_defaults()) expected_logoutput = ('Random log output\n' 'Some other log output\n' 'Another application log output.\n') logoutput = expected_logoutput + ( 'Displayed com.google.fpl.libfplutil_test/' 'android.app.NativeActivity\n' 'Line noise\n') # Configure the set of expected commands executed by run_android_apk. os.path.exists = lambda unused_filename: True build_environment.sdk_home = 'sdk_home' adb_path = build_environment._find_binary(android.BuildEnvironment.ADB) run_command_mock = common_test.RunCommandMockList([ common_test.RunCommandMock( self, args='%s devices -l' % adb_path, stdout=( 'List of devices attached\n' '123456\tdevice\tusb:2-3.4\tproduct:razor\tmodel:Nexus_7\t' 'device:flo\n')), common_test.RunCommandMock(self, args='%s -s 123456 logcat -c' % adb_path), common_test.RunCommandMock( self, args=('%s -s 123456 shell am start -S -n ' 'com.google.fpl.libfplutil_test/' 'android.app.NativeActivity' % adb_path)) ]) build_environment.run_subprocess = run_command_mock subprocess.Popen = SubprocessMockStdOut( self, args=[adb_path, '-s', '123456', 'logcat'], stdout=logoutput) self.assertEquals( expected_logoutput, build_environment.run_android_apk(adb_device='123456'))
def test_clean_libraries(self): d = android.BuildEnvironment.build_defaults() b = android.BuildEnvironment(d) b.clean = True m = common_test.RunCommandMock(self) b.run_subprocess = m ndk_build = os.path.join(b.ndk_home, 'ndk-build') l = 'libfoo' lpath = os.path.abspath(os.path.join(b.project_directory, l)) expect = [ ndk_build, '-j' + str(platform.mac_ver()[0] and 1 or b.cpu_count), '-C', lpath, 'clean' ] m.expect(expect) b.build_android_libraries([l]) b.verbose = True expect.append('V=1') m.expect(expect) b.build_android_libraries([l]) expect.append('NDK_OUT=%s' % lpath) m.expect(expect) b.build_android_libraries([l], output=l) b.make_flags = '-DFOO -DBAR -DBAZ' flaglist = ['-DFOO', '-DBAR', '-DBAZ'] expect += flaglist m.expect(expect) b.build_android_libraries([l], output=l) common._find_executable = self.find_executable b.ndk_home = '/dev/null' with self.assertRaises(common.ToolPathError): b.build_android_libraries([l], output=l) b._parse(f)
def test_check_adb_devices_no_devices(self): build_environment = android.BuildEnvironment( android.BuildEnvironment.build_defaults()) build_environment.sdk_home = 'sdk_home' os.path.exists = lambda unused_filename: True build_environment.run_subprocess = common_test.RunCommandMock( self, args='%s devices -l' % (build_environment._find_binary(android.BuildEnvironment.ADB)), stdout='List of devices attached\n\n', shell=True) with self.assertRaises(common.AdbError): build_environment.check_adb_devices()
def test_clean_android_apk(self): build_environment, manifest, buildxml_filename = ( self._create_update_build_xml_setup()) os.path.exists = lambda unused_filename: True os.path.getmtime = lambda unused_filename: 1 build_environment.clean = True build_environment.ant_path = 'ant' run_command_mock = common_test.RunCommandMock(self) run_command_mock.expect(['ant', 'clean', '-quiet'], None) build_environment.run_subprocess = run_command_mock build_environment.ndk_home = 'ndk_path' build_environment.sdk_home = 'sdk_path' build_environment.build_android_apk(manifest=manifest)
def test_build_android_apk_unsigned(self): build_environment, manifest, buildxml_filename = ( self._create_update_build_xml_setup()) os.path.exists = lambda unused_filename: True os.path.getmtime = lambda unused_filename: 1 build_environment.ant_path = 'ant' build_environment.ant_flags = 'a b "c d"' run_command_mock = common_test.RunCommandMock(self) run_command_mock.expect(['ant', 'release', 'a', 'b', 'c d'], None) build_environment.run_subprocess = run_command_mock build_environment.ndk_home = 'ndk_path' build_environment.sdk_home = 'sdk_path' build_environment.build_android_apk(manifest=manifest)
def test_check_adb_devices_one_device(self): build_environment = android.BuildEnvironment( android.BuildEnvironment.build_defaults()) build_environment.sdk_home = 'sdk_home' os.path.exists = lambda unused_filename: True build_environment.run_subprocess = common_test.RunCommandMock( self, args='%s devices -l' % (build_environment._find_binary(android.BuildEnvironment.ADB)), stdout=('List of devices attached\n' '06d8bd43 device usb:2-3.3 product:razor ' 'model:Nexus_7 device:flo\n'), shell=True) device = build_environment.check_adb_devices() self.assertEquals('06d8bd43', device.serial)
def test_check_adb_devices_specified_device_found(self): build_environment = android.BuildEnvironment( android.BuildEnvironment.build_defaults()) build_environment.sdk_home = 'sdk_home' os.path.exists = lambda unused_filename: True build_environment.run_subprocess = common_test.RunCommandMock( self, args='%s devices -l' % (build_environment._find_binary(android.BuildEnvironment.ADB)), stdout=('List of devices attached\n' '06d8bd43 device usb:2-3.3 product:razor ' 'model:Nexus_7 device:flo\n' '98aaffe123 device usb:2-3.3 product:fishy ' 'model:Nexus_55 device:jelly\n'), shell=True) build_environment.check_adb_devices('98aaffe123')
def test_check_adb_devices_multiple_devices(self): build_environment = android.BuildEnvironment( android.BuildEnvironment.build_defaults()) build_environment.sdk_home = 'sdk_home' os.path.exists = lambda unused_filename: True build_environment.run_subprocess = common_test.RunCommandMock( self, args='%s devices -l' % (build_environment._find_binary(android.BuildEnvironment.ADB)), stdout=('Stuff to ignore\n' 'More stuff to ignore\n' 'List of devices attached\n' '06d8bd43 device usb:2-3.3 product:razor ' 'model:Nexus_7 device:flo\n' '98aaffe123 device usb:2-3.3 product:fishy ' 'model:Nexus_55 device:jelly\n'), shell=True) with self.assertRaises(common.AdbError): build_environment.check_adb_devices()
def test_list_installed_packages(self): build_environment = android.BuildEnvironment( android.BuildEnvironment.build_defaults()) build_environment.sdk_home = 'sdk_home' os.path.exists = lambda unused_filename: True adb_path = build_environment._find_binary(android.BuildEnvironment.ADB) build_environment.run_subprocess = common_test.RunCommandMock( self, args='%s -s 06d8bd43 shell pm list packages' % adb_path, stdout=('package:com.google.earth\n' 'junk\n' 'package:com.google.android.gsf\n' 'package:com.android.keyguard\n' 'nothing useful\n')) expected = [ 'com.google.earth', 'com.google.android.gsf', 'com.android.keyguard' ] self.assertListEqual( expected, build_environment.list_installed_packages('06d8bd43'))
def test_create_update_build_xml_update(self): build_environment, manifest, buildxml_filename = ( self._create_update_build_xml_setup()) os.path.exists = lambda unused_filename: True os.path.getmtime = lambda filename: ({ manifest.path: 10, buildxml_filename: 5 }[filename]) run_command_mock = common_test.RunCommandMock(self) run_command_mock.expect([ os.path.join('sdk_path', 'tools', 'android'), 'update', 'project', '--path', build_environment.get_project_directory(), '--target', 10, '--name', 'libfplutil_test' ]) build_environment.run_subprocess = run_command_mock build_environment.ndk_home = 'ndk_path' build_environment.sdk_home = 'sdk_path' buildxml = build_environment.create_update_build_xml(manifest) self.assertEqual('libfplutil_test', buildxml.project_name)
def test_find_android_sdk(self): d = android.BuildEnvironment.build_defaults() b = android.BuildEnvironment(d) m = common_test.RunCommandMock(self) b.run_subprocess = m expect = ['android', 'list', 'target', '--compact'] m.expect(expect) m.returns('android-3\n' 'android-5\n' 'meaningless\n' 'android-10\n' 'android-L\n') got = b._find_best_android_sdk('android', 1, 5) self.assertEqual(got, 'android-5') got = b._find_best_android_sdk('android', 5, 15) self.assertEqual(got, 'android-10') got = b._find_best_android_sdk('android', 1, 2) self.assertEqual(got, 'android-10') with self.assertRaises(common.ConfigurationError): b._find_best_android_sdk('android', 11, 20) m.returns('android-10\n' 'android-15\n' 'android-7\n') got = b._find_best_android_sdk('android', 5, 15) self.assertEqual(got, 'android-15')
def test_install_android_apk(self): class MockBuildEnvironment(android.BuildEnvironment): def __init__(self, defaults): android.BuildEnvironment.__init__(self, defaults) def parse_manifest(self, path=None): manifest = android.AndroidManifest(None) manifest._parse( FileMock( '<manifest ' ' xmlns:android="http://schemas.android.com/apk/res/android"\n' ' package="com.google.fpl.libfplutil_test">\n' ' <uses-sdk android:minSdkVersion="1"/>\n' ' <application>\n' ' <activity android:name="android.app.NativeActivity">\n' ' <meta-data android:name="android.app.lib_name"\n' ' android:value="test"/>\n' ' </activity>\n' ' </application>\n' '</manifest>')) return manifest def create_update_build_xml(self, manifest, path=None): buildxml = android.BuildXml(None) buildxml._parse( FileMock( '<?xml version="1.0" encoding="UTF-8"?>\n' '<project name="libfplutil_test" default="help">\n' '</project>\n')) return buildxml def list_installed_packages(self, adb_device=None, check_devices=True): return [ 'com.google.earth', 'com.google.android.gsf', 'com.google.fpl.libfplutil_test', 'com.android.keyguard' ] build_environment = MockBuildEnvironment( android.BuildEnvironment.build_defaults()) build_environment.sdk_home = '' os.path.exists = lambda unused_filename: True build_environment.sdk_home = 'sdk_home' adb_path = build_environment._find_binary(android.BuildEnvironment.ADB) build_environment.run_subprocess = common_test.RunCommandMockList([ common_test.RunCommandMock( self, args='%s devices -l' % adb_path, stdout=( 'List of devices attached\n' '06d8bd43 device usb:2-3.3 product:razor ' 'model:Nexus_7 device:flo\n')), common_test.RunCommandMock(self, args=('%s -s 06d8bd43 uninstall ' 'com.google.fpl.libfplutil_test' % adb_path)), common_test.RunCommandMock( self, args=('%s -s 06d8bd43 install %s' % (adb_path, os.path.join(build_environment.get_project_directory(), 'bin', 'libfplutil_test.apk')))) ]) build_environment.install_android_apk(adb_device='06d8bd43')