diff --git a/packages/flutter_tools/lib/src/device.dart b/packages/flutter_tools/lib/src/device.dart index ed1d9ae536..418ba521df 100644 --- a/packages/flutter_tools/lib/src/device.dart +++ b/packages/flutter_tools/lib/src/device.dart @@ -56,6 +56,12 @@ abstract class _Device { /// Check if the current version of the given app is already installed bool isAppInstalled(ApplicationPackage app); + + /// Start an app package on the current device + Future startApp(ApplicationPackage app); + + /// Stop an app package on the current device + Future stopApp(ApplicationPackage app); } class IOSDevice extends _Device { @@ -80,6 +86,9 @@ class IOSDevice extends _Device { String _informerPath; String get informerPath => _informerPath; + String _debuggerPath; + String get debuggerPath => _debuggerPath; + String _name; String get name => _name; @@ -93,6 +102,7 @@ class IOSDevice extends _Device { _installerPath = _checkForCommand('ideviceinstaller'); _listerPath = _checkForCommand('idevice_id'); _informerPath = _checkForCommand('ideviceinfo'); + _debuggerPath = _checkForCommand('idevicedebug'); } static List getAttachedDevices([IOSDevice mockIOS]) { @@ -160,6 +170,37 @@ class IOSDevice extends _Device { @override bool isAppInstalled(ApplicationPackage app) { + try { + String apps = runCheckedSync([installerPath, '-l']); + if (new RegExp(app.appPackageID, multiLine: true).hasMatch(apps)) { + return true; + } + } catch (e) { + return false; + } + return false; + } + + @override + Future startApp(ApplicationPackage app) async { + if (!isAppInstalled(app)) { + return false; + } + // idevicedebug hangs forever after launching the app, so kill it after + // giving it plenty of time to send the launch command. + return runAndKill( + [debuggerPath, 'run', app.appPackageID], new Duration(seconds: 3)).then( + (_) { + return true; + }, onError: (e) { + _logging.info('Failure running $debuggerPath: ', e); + return false; + }); + } + + @override + Future stopApp(ApplicationPackage app) async { + // Currently we don't have a way to stop an app running on iOS. return false; } } @@ -458,7 +499,14 @@ class AndroidDevice extends _Device { return true; } - bool stop(AndroidApk apk) { + @override + Future startApp(AndroidApk apk) async { + // Android currently has to be started with startServer(...). + assert(false); + return false; + } + + Future stopApp(AndroidApk apk) async { // Turn off reverse port forwarding runSync([adbPath, 'reverse', '--remove', 'tcp:$_serverPort']); // Stop the app diff --git a/packages/flutter_tools/lib/src/process.dart b/packages/flutter_tools/lib/src/process.dart index c703571bfe..b0402505bb 100644 --- a/packages/flutter_tools/lib/src/process.dart +++ b/packages/flutter_tools/lib/src/process.dart @@ -28,6 +28,18 @@ Future runCommandAndStreamOutput(List cmd, return proc.exitCode; } +Future runAndKill(List cmd, Duration timeout) async { + _logging.info(cmd.join(' ')); + Future proc = Process.start( + cmd[0], cmd.getRange(1, cmd.length).toList(), + mode: ProcessStartMode.DETACHED); + + return new Future.delayed(timeout, () async { + _logging.info('Intentionally killing ${cmd[0]}'); + Process.killPid((await proc).pid); + }); +} + /// Run cmd and return stdout. /// Throws an error if cmd exits with a non-zero value. String runCheckedSync(List cmd) => diff --git a/packages/flutter_tools/lib/src/start.dart b/packages/flutter_tools/lib/src/start.dart index 08af1a186d..8d2dbc0dc2 100644 --- a/packages/flutter_tools/lib/src/start.dart +++ b/packages/flutter_tools/lib/src/start.dart @@ -41,22 +41,26 @@ class StartCommand extends Command { if (android == null) { android = new AndroidDevice(); } + if (ios == null) { + ios = new IOSDevice(); + } bool startedSomewhere = false; bool poke = argResults['poke']; if (!poke) { - StopCommand stopper = new StopCommand(android); + StopCommand stopper = new StopCommand(android: android, ios: ios); stopper.stop(); // Only install if the user did not specify a poke InstallCommand installer = new InstallCommand(android: android, ios: ios); - startedSomewhere = installer.install(); + installer.install(); } + Map packages = + ApplicationPackageFactory.getAvailableApplicationPackages(); + bool startedOnAndroid = false; if (android.isConnected()) { - Map packages = - ApplicationPackageFactory.getAvailableApplicationPackages(); ApplicationPackage androidApp = packages[BuildPlatform.android]; String target = path.absolute(argResults['target']); @@ -64,6 +68,12 @@ class StartCommand extends Command { target, poke, argResults['checked'], androidApp); } + if (ios.isConnected()) { + ApplicationPackage iosApp = packages[BuildPlatform.iOS]; + + startedSomewhere = await ios.startApp(iosApp) || startedSomewhere; + } + if (startedSomewhere || startedOnAndroid) { return 0; } else { diff --git a/packages/flutter_tools/lib/src/stop.dart b/packages/flutter_tools/lib/src/stop.dart index c3edaf6d29..d1f9844f0e 100644 --- a/packages/flutter_tools/lib/src/stop.dart +++ b/packages/flutter_tools/lib/src/stop.dart @@ -17,29 +17,39 @@ class StopCommand extends Command { final name = 'stop'; final description = 'Stop your Flutter app on all attached devices.'; AndroidDevice android = null; + IOSDevice ios = null; - StopCommand([this.android]); + StopCommand({this.android, this.ios}); @override Future run() async { - if (android == null) { - android = new AndroidDevice(); - } - - if (stop()) { + if (await stop()) { return 0; } else { return 2; } } - bool stop() { + Future stop() async { + if (android == null) { + android = new AndroidDevice(); + } + if (ios == null) { + ios = new IOSDevice(); + } + bool stoppedSomething = false; + Map packages = + ApplicationPackageFactory.getAvailableApplicationPackages(); + if (android.isConnected()) { - Map packages = - ApplicationPackageFactory.getAvailableApplicationPackages(); ApplicationPackage androidApp = packages[BuildPlatform.android]; - stoppedSomething = android.stop(androidApp) || stoppedSomething; + stoppedSomething = await android.stopApp(androidApp) || stoppedSomething; + } + + if (ios.isConnected()) { + ApplicationPackage iosApp = packages[BuildPlatform.iOS]; + stoppedSomething = await ios.stopApp(iosApp) || stoppedSomething; } return stoppedSomething; diff --git a/packages/flutter_tools/test/start_test.dart b/packages/flutter_tools/test/start_test.dart index 732f18b29d..9ddfb0e999 100644 --- a/packages/flutter_tools/test/start_test.dart +++ b/packages/flutter_tools/test/start_test.dart @@ -21,11 +21,36 @@ defineTests() { MockAndroidDevice android = new MockAndroidDevice(); when(android.isConnected()).thenReturn(true); when(android.installApp(any)).thenReturn(true); - when(android.stop(any)).thenReturn(true); + when(android.startServer(any, any, any, any)).thenReturn(true); + when(android.stopApp(any)).thenReturn(true); MockIOSDevice ios = new MockIOSDevice(); when(ios.isConnected()).thenReturn(false); when(ios.installApp(any)).thenReturn(false); + when(ios.startApp(any)).thenReturn(false); + when(ios.startApp(any)).thenReturn(false); + + StartCommand command = new StartCommand(android: android, ios: ios); + + CommandRunner runner = new CommandRunner('test_flutter', '') + ..addCommand(command); + runner.run(['start']).then((int code) => expect(code, equals(0))); + }); + + test('returns 0 when iOS is connected and ready to be started', () { + applicationPackageSetup(); + + MockAndroidDevice android = new MockAndroidDevice(); + when(android.isConnected()).thenReturn(false); + when(android.installApp(any)).thenReturn(false); + when(android.startServer(any, any, any, any)).thenReturn(false); + when(android.stopApp(any)).thenReturn(false); + + MockIOSDevice ios = new MockIOSDevice(); + when(ios.isConnected()).thenReturn(true); + when(ios.installApp(any)).thenReturn(true); + when(ios.startApp(any)).thenReturn(true); + when(ios.stopApp(any)).thenReturn(false); StartCommand command = new StartCommand(android: android, ios: ios); diff --git a/packages/flutter_tools/test/stop_test.dart b/packages/flutter_tools/test/stop_test.dart index af6ec19159..8d67d662bd 100644 --- a/packages/flutter_tools/test/stop_test.dart +++ b/packages/flutter_tools/test/stop_test.dart @@ -20,8 +20,31 @@ defineTests() { MockAndroidDevice android = new MockAndroidDevice(); when(android.isConnected()).thenReturn(true); - when(android.stop(any)).thenReturn(true); - StopCommand command = new StopCommand(android); + when(android.stopApp(any)).thenReturn(true); + + MockIOSDevice ios = new MockIOSDevice(); + when(ios.isConnected()).thenReturn(false); + when(ios.stopApp(any)).thenReturn(false); + + StopCommand command = new StopCommand(android: android, ios: ios); + + CommandRunner runner = new CommandRunner('test_flutter', '') + ..addCommand(command); + runner.run(['stop']).then((int code) => expect(code, equals(0))); + }); + + test('returns 0 when iOS is connected and ready to be stopped', () { + applicationPackageSetup(); + + MockAndroidDevice android = new MockAndroidDevice(); + when(android.isConnected()).thenReturn(false); + when(android.stopApp(any)).thenReturn(false); + + MockIOSDevice ios = new MockIOSDevice(); + when(ios.isConnected()).thenReturn(true); + when(ios.stopApp(any)).thenReturn(true); + + StopCommand command = new StopCommand(android: android, ios: ios); CommandRunner runner = new CommandRunner('test_flutter', '') ..addCommand(command);