197 lines
6.7 KiB
Dart
197 lines
6.7 KiB
Dart
import 'dart:async';
|
|
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:material_ui/material_ui.dart';
|
|
import 'package:window_manager/window_manager.dart';
|
|
import 'package:ads_manager/data/manager_repository.dart';
|
|
import 'package:ads_manager/ui/embedded_screen.dart';
|
|
|
|
Future<void> main() async {
|
|
WidgetsFlutterBinding.ensureInitialized();
|
|
await windowManager.ensureInitialized();
|
|
final repo = ManagerRepository(stdout.writeln);
|
|
Process? screen;
|
|
Session? usbSession;
|
|
final report = <String, dynamic>{};
|
|
try {
|
|
await repo.init();
|
|
var serial = const String.fromEnvironment('ADS_EMBED_SERIAL');
|
|
if (const bool.fromEnvironment('ADS_EMBED_USB')) {
|
|
repo.settings.usbMode = true;
|
|
final idle = (await repo.devices())
|
|
.where((device) => !device.busy)
|
|
.firstOrNull;
|
|
if (idle == null) {
|
|
throw StateError('No idle device for USB embedding smoke');
|
|
}
|
|
final session = await repo.connect(idle.id);
|
|
usbSession = session;
|
|
for (var i = 0; i < 20 && !session.ready; i++) {
|
|
await Future<void>.delayed(const Duration(seconds: 3));
|
|
await repo.monitor();
|
|
}
|
|
if (!session.ready) {
|
|
throw StateError('Virtual USB ADB did not become ready');
|
|
}
|
|
serial = session.endpoint;
|
|
}
|
|
if (serial.isEmpty) {
|
|
throw StateError('Specify --dart-define=ADS_EMBED_SERIAL=...');
|
|
}
|
|
screen = await repo.launchScreen(
|
|
serial,
|
|
repo.settings.adb,
|
|
'native-smoke',
|
|
embedded: true,
|
|
);
|
|
if (usbSession != null) {
|
|
usbSession.screen = screen;
|
|
usbSession.screenEmbedded = true;
|
|
}
|
|
repo.watch(screen, 'native-smoke');
|
|
unawaited(
|
|
screen.exitCode.then((code) => stdout.writeln("SCRCPY EXIT=$code")),
|
|
);
|
|
final pid = screen.pid;
|
|
stdout.writeln("SCRCPY PID=$pid");
|
|
final show = ValueNotifier(true);
|
|
runApp(
|
|
MaterialApp(
|
|
home: Scaffold(
|
|
body: Padding(
|
|
padding: const EdgeInsets.fromLTRB(220, 80, 24, 24),
|
|
child: ValueListenableBuilder<bool>(
|
|
valueListenable: show,
|
|
builder: (_, visible, child) => visible
|
|
? EmbeddedScreen(key: ValueKey(pid), pid: pid)
|
|
: const SizedBox(),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
await windowManager.waitUntilReadyToShow(
|
|
const WindowOptions(size: Size(1280, 820), title: 'ADS Embedded Smoke'),
|
|
() async {
|
|
await windowManager.show();
|
|
},
|
|
);
|
|
Future<Map<String, dynamic>> state() async => Map<String, dynamic>.from(
|
|
await EmbeddedScreen.channel.invokeMethod<Map<Object?, Object?>>(
|
|
'state',
|
|
{'pid': pid},
|
|
) ??
|
|
{},
|
|
);
|
|
for (var i = 0; i < 60; i++) {
|
|
await Future<void>.delayed(const Duration(milliseconds: 500));
|
|
if ((await state())['visible'] == true) {
|
|
break;
|
|
}
|
|
}
|
|
report['initial'] = await state();
|
|
if (report['initial']['embedded'] != true ||
|
|
report['initial']['visible'] != true) {
|
|
throw StateError('Embedding failed: $report');
|
|
}
|
|
await windowManager.setSize(const Size(1080, 720));
|
|
await Future<void>.delayed(const Duration(seconds: 2));
|
|
report['resized'] = await state();
|
|
if (report['resized']['width'] == report['initial']['width']) {
|
|
throw StateError('Resize did not reach child');
|
|
}
|
|
show.value = false;
|
|
await Future<void>.delayed(const Duration(seconds: 1));
|
|
report['pageHidden'] = await state();
|
|
if (report['pageHidden']['visible'] != false) {
|
|
throw StateError('Page hide failed');
|
|
}
|
|
show.value = true;
|
|
await Future<void>.delayed(const Duration(seconds: 1));
|
|
await windowManager.hide();
|
|
await Future<void>.delayed(const Duration(seconds: 1));
|
|
report['trayHidden'] = await state();
|
|
if (report['trayHidden']['visible'] != false) {
|
|
throw StateError('Host hide failed');
|
|
}
|
|
final duplicate = await Process.start(Platform.resolvedExecutable, []);
|
|
final duplicateExit = await duplicate.exitCode.timeout(
|
|
const Duration(seconds: 10),
|
|
);
|
|
if (duplicateExit != 0) {
|
|
throw StateError('Duplicate instance failed to exit');
|
|
}
|
|
await Future<void>.delayed(const Duration(seconds: 1));
|
|
report['duplicateExited'] = duplicateExit;
|
|
if (!await windowManager.isVisible()) {
|
|
throw StateError('Duplicate did not restore existing tray window');
|
|
}
|
|
report['restored'] = await state();
|
|
if (report['restored']['visible'] != true) {
|
|
throw StateError('Restore failed');
|
|
}
|
|
await EmbeddedScreen.channel.invokeMethod<void>('release', {'pid': pid});
|
|
report['released'] = await state();
|
|
if (report['released']['embedded'] != false) {
|
|
throw StateError('Release failed');
|
|
}
|
|
if (usbSession != null) {
|
|
final tunnelPid = usbSession.process.pid;
|
|
final bridgePid = usbSession.usb!.bridge!.pid;
|
|
await repo.screen(usbSession, embedded: false);
|
|
var standaloneVisible = false;
|
|
for (var i = 0; i < 30; i++) {
|
|
await Future<void>.delayed(const Duration(milliseconds: 500));
|
|
final child = usbSession.screen;
|
|
if (child == null) {
|
|
throw StateError('Standalone scrcpy exited');
|
|
}
|
|
standaloneVisible =
|
|
await EmbeddedScreen.channel.invokeMethod<bool>(
|
|
'standaloneVisible',
|
|
{'pid': child.pid},
|
|
) ??
|
|
false;
|
|
if (standaloneVisible) {
|
|
break;
|
|
}
|
|
}
|
|
if (!standaloneVisible) {
|
|
throw StateError('Standalone window did not appear');
|
|
}
|
|
report['standaloneVisible'] = true;
|
|
if (usbSession.screenEmbedded || usbSession.screen?.pid == pid) {
|
|
throw StateError('Standalone mode did not restart scrcpy');
|
|
}
|
|
if (usbSession.process.pid != tunnelPid ||
|
|
usbSession.usb!.bridge!.pid != bridgePid) {
|
|
throw StateError('Screen switch changed device connection');
|
|
}
|
|
final state = await repo.run(usbSession.adb, [
|
|
'-s',
|
|
usbSession.endpoint,
|
|
'get-state',
|
|
]);
|
|
if (state.trim() != 'device') {
|
|
throw StateError('USB connection lost during screen switch');
|
|
}
|
|
report['standalonePreservesUsb'] = true;
|
|
}
|
|
report['passed'] = true;
|
|
} catch (e, stack) {
|
|
report['passed'] = false;
|
|
report['error'] = '$e';
|
|
stderr.writeln('$e\n$stack');
|
|
} finally {
|
|
await repo.stop(screen);
|
|
await repo.shutdown();
|
|
final root = repo.root?.path ?? Directory.current.parent.path;
|
|
await File('$root/work/flutter-embed-smoke.json')
|
|
.writeAsString(const JsonEncoder.withIndent(' ').convert(report));
|
|
stdout.writeln(jsonEncode(report));
|
|
await windowManager.destroy();
|
|
exit(report['passed'] == true ? 0 : 1);
|
|
}
|
|
}
|