iPad layout review notes and native fixture host
Run the full-app fixture entry on an iOS simulator: options from a file in the app's tmp dir or the environment, route via a route provider, a narrow strip for Split View widths, and a small in-app tap/type driver.
This commit is contained in:
@@ -0,0 +1,165 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/gestures.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
|
||||
/// A tiny in-app driver for simulator captures, where `simctl` cannot tap.
|
||||
///
|
||||
/// [script] is `;`-separated steps run after the first frame, 700 ms apart:
|
||||
/// `wait:<ms>`, `tap:<label>` (a Text, Tooltip, Semantics label or field
|
||||
/// hint; `tap:<label>#<n>` picks the n-th match), `tapxy:<x>,<y>`,
|
||||
/// `drag:<x>,<y>,<dx>,<dy>`, `type:<text>` (into the focused field; `\n`
|
||||
/// is a newline), `orient:landscape|portrait` and `labels` (prints the
|
||||
/// visible labels with their centres to the device log).
|
||||
void runFixtureSteps(String script) {
|
||||
final steps = [
|
||||
for (final step in script.split(';'))
|
||||
if (step.trim().isNotEmpty) step.trim(),
|
||||
];
|
||||
if (steps.isEmpty) return;
|
||||
WidgetsBinding.instance.waitUntilFirstFrameRasterized.then((_) async {
|
||||
await Future<void>.delayed(const Duration(milliseconds: 1500));
|
||||
for (final step in steps) {
|
||||
final colon = step.indexOf(':');
|
||||
final verb = colon < 0 ? step : step.substring(0, colon);
|
||||
final value = colon < 0 ? '' : step.substring(colon + 1);
|
||||
try {
|
||||
await _run(verb, value);
|
||||
} catch (error) {
|
||||
// ignore: avoid_print
|
||||
print('FIXTURE_STEP_FAILED $step: $error');
|
||||
}
|
||||
await Future<void>.delayed(const Duration(milliseconds: 700));
|
||||
}
|
||||
// ignore: avoid_print
|
||||
print('FIXTURE_STEPS_DONE');
|
||||
});
|
||||
}
|
||||
|
||||
Future<void> _run(String verb, String value) async {
|
||||
switch (verb) {
|
||||
case 'wait':
|
||||
await Future<void>.delayed(Duration(milliseconds: int.parse(value)));
|
||||
case 'tap':
|
||||
final hash = value.lastIndexOf('#');
|
||||
final index = hash < 0 ? 0 : int.parse(value.substring(hash + 1));
|
||||
final label = hash < 0 ? value : value.substring(0, hash);
|
||||
final hits = _find(label);
|
||||
if (hits.length <= index) throw StateError('no "$label"');
|
||||
await _tap(hits[index].center);
|
||||
case 'tapxy':
|
||||
final parts = value.split(',').map(double.parse).toList();
|
||||
await _tap(Offset(parts[0], parts[1]));
|
||||
case 'drag':
|
||||
final p = value.split(',').map(double.parse).toList();
|
||||
await _drag(Offset(p[0], p[1]), Offset(p[2], p[3]));
|
||||
case 'type':
|
||||
_type(value.replaceAll(r'\n', '\n'));
|
||||
case 'orient':
|
||||
await SystemChrome.setPreferredOrientations(
|
||||
value == 'landscape'
|
||||
? [DeviceOrientation.landscapeLeft]
|
||||
: [DeviceOrientation.portraitUp],
|
||||
);
|
||||
case 'labels':
|
||||
for (final hit in _find(null)) {
|
||||
// ignore: avoid_print
|
||||
print('FIXTURE_LABEL ${hit.label} @ ${hit.center}');
|
||||
}
|
||||
default:
|
||||
throw StateError('unknown step');
|
||||
}
|
||||
}
|
||||
|
||||
class _Hit {
|
||||
_Hit(this.label, this.center);
|
||||
final String label;
|
||||
final Offset center;
|
||||
}
|
||||
|
||||
String? _labelOf(Widget widget) => switch (widget) {
|
||||
Text(:final data?) => data,
|
||||
Text(:final textSpan?) => textSpan.toPlainText(),
|
||||
Tooltip(:final message?) => message,
|
||||
Semantics(:final properties) => properties.label,
|
||||
Icon(:final semanticLabel?) => semanticLabel,
|
||||
InputDecorator(:final decoration) =>
|
||||
decoration.hintText ?? decoration.labelText,
|
||||
_ => null,
|
||||
};
|
||||
|
||||
/// Visible widgets labelled [label] (every labelled one when null).
|
||||
List<_Hit> _find(String? label) {
|
||||
final hits = <_Hit>[];
|
||||
final size = WidgetsBinding.instance.renderViews.first.size;
|
||||
void visit(Element element) {
|
||||
final text = _labelOf(element.widget);
|
||||
if (text != null && (label == null || text == label)) {
|
||||
final box = element.findRenderObject();
|
||||
if (box is RenderBox && box.attached && box.hasSize) {
|
||||
final center = box.localToGlobal(box.size.center(Offset.zero));
|
||||
if ((Offset.zero & size).contains(center)) hits.add(_Hit(text, center));
|
||||
}
|
||||
}
|
||||
element.visitChildren(visit);
|
||||
}
|
||||
|
||||
WidgetsBinding.instance.rootElement?.visitChildren(visit);
|
||||
return hits;
|
||||
}
|
||||
|
||||
var _pointer = 900;
|
||||
|
||||
Future<void> _tap(Offset at) async {
|
||||
final pointer = ++_pointer;
|
||||
final binding = GestureBinding.instance;
|
||||
binding.handlePointerEvent(PointerDownEvent(pointer: pointer, position: at));
|
||||
await Future<void>.delayed(const Duration(milliseconds: 60));
|
||||
binding.handlePointerEvent(PointerUpEvent(pointer: pointer, position: at));
|
||||
}
|
||||
|
||||
Future<void> _drag(Offset from, Offset by) async {
|
||||
final pointer = ++_pointer;
|
||||
final binding = GestureBinding.instance;
|
||||
binding.handlePointerEvent(
|
||||
PointerDownEvent(pointer: pointer, position: from),
|
||||
);
|
||||
const moves = 16;
|
||||
for (var i = 1; i <= moves; i++) {
|
||||
await Future<void>.delayed(const Duration(milliseconds: 16));
|
||||
binding.handlePointerEvent(
|
||||
PointerMoveEvent(
|
||||
pointer: pointer,
|
||||
position: from + by * (i / moves),
|
||||
delta: by / moves.toDouble(),
|
||||
),
|
||||
);
|
||||
}
|
||||
binding.handlePointerEvent(
|
||||
PointerUpEvent(pointer: pointer, position: from + by),
|
||||
);
|
||||
}
|
||||
|
||||
void _type(String text) {
|
||||
EditableTextState? focused;
|
||||
void visit(Element element) {
|
||||
if (element is StatefulElement &&
|
||||
element.state is EditableTextState &&
|
||||
(element.widget as EditableText).focusNode.hasFocus) {
|
||||
focused = element.state as EditableTextState;
|
||||
}
|
||||
element.visitChildren(visit);
|
||||
}
|
||||
|
||||
WidgetsBinding.instance.rootElement?.visitChildren(visit);
|
||||
final state = focused;
|
||||
if (state == null) throw StateError('no focused field');
|
||||
state.userUpdateTextEditingValue(
|
||||
TextEditingValue(
|
||||
text: text,
|
||||
selection: TextSelection.collapsed(offset: text.length),
|
||||
),
|
||||
SelectionChangedCause.keyboard,
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user