dropRedundantNestedObserveLeaves function
Drop host/leaf duplicates that survived the keep walk.
Examples: icon → child icon (IconButton + Icon), button → caption text
that only repeats the button title, card → title text matching the card.
Implementation
List<({Element element, UiElement ui})> dropRedundantNestedObserveLeaves(
List<({Element element, UiElement ui})> kept,
) {
return RunnerBenchmark.sync('observer', 'dropRedundantNestedObserveLeaves',
() {
if (kept.length < 2) return kept;
final elementToIndex = <Element, int>{
for (var i = 0; i < kept.length; i++) kept[i].element: i,
};
final drop = <int>{};
// The generated Ensemble app root and the authored screen root can be
// separate Flutter branches while exposing the same full-screen bounds.
// Remove the generated entry when the report would otherwise show the
// identical visual widget twice. This must be checked across the snapshot,
// not only along Flutter ancestry.
for (var i = 0; i < kept.length; i++) {
final generatedId = kept[i].ui.testId;
if (!_isGeneratedEnsembleAppId(generatedId)) continue;
for (var j = 0; j < kept.length; j++) {
if (i == j) continue;
final authored = kept[j].ui;
if (authored.testId != null &&
authored.testId!.trim().isNotEmpty &&
!_isGeneratedEnsembleAppId(authored.testId) &&
kept[i].ui.type == authored.type &&
_sameObserveBounds(kept[i].ui.bounds, authored.bounds)) {
drop.add(i);
break;
}
}
}
for (var i = 0; i < kept.length; i++) {
if (drop.contains(i)) continue;
final child = kept[i].ui;
UiElement? parentUi;
int? parentIndex;
kept[i].element.visitAncestorElements((ancestor) {
final idx = elementToIndex[ancestor];
if (idx == null || drop.contains(idx)) return true;
parentIndex = idx;
parentUi = kept[idx].ui;
return false;
});
if (parentUi == null || parentIndex == null) continue;
final pType = parentUi!.type;
final cType = child.type;
// Keyed leaves are locator targets — never collapse them away.
final childKeyed =
child.testId != null && child.testId!.trim().isNotEmpty;
final parentKeyed =
parentUi!.testId != null && parentUi!.testId!.trim().isNotEmpty;
// Ensemble can expose the same authored id on its keyed host and the
// keyed widget it wraps. When both nodes describe the exact same visual
// bounds, retain the outer owner once instead of publishing a duplicate
// locator path (for example widget sports_0 > widget sports_0).
if (parentKeyed &&
childKeyed &&
parentUi!.testId == child.testId &&
pType == cType &&
_sameObserveBounds(parentUi!.bounds, child.bounds)) {
drop.add(i);
continue;
}
// Ensemble text widgets expose a keyed host that is classified as text
// from its rendered Text descendant. Keep the host as the locator and
// avoid publishing that same caption a second time as a child. A child
// with its own testId remains an independent locator and is preserved.
if (pType == 'text' &&
cType == 'text' &&
!childKeyed &&
_sameObserveText(parentUi!, child)) {
drop.add(i);
continue;
}
if (pType == 'icon' && (cType == 'icon' || cType == 'text')) {
if (!childKeyed) drop.add(i);
continue;
}
if ((pType == 'button' || pType == 'dropdown') && cType == 'text') {
if (!childKeyed && _sameObserveCaption(parentUi!, child)) drop.add(i);
continue;
}
// Trailing arrows on LabelArrowButton / compact CTAs — decorative chrome;
// tap the button, not a selector-less nested icon. Dropdown expand
// chevrons stay visible; status icons under cards stay too.
if (pType == 'button' && cType == 'icon') {
if (!childKeyed && child.state.interactable != true) drop.add(i);
continue;
}
// AppIcon / inline SVG under a password row must not linger as a
// selector-less `image` leaf when the eye affordance is the icon host.
if (pType == 'button' &&
(cType == 'image' ||
cType == 'svg' ||
cType == 'gif' ||
cType == 'lottie')) {
if (!childKeyed && child.state.interactable != true) drop.add(i);
continue;
}
// KeyedSubtree(testId) + child InkWell both observe as button — keep the
// keyed host only. Also collapse nested WifiCard show-password InkWell
// under the outer "Wachtwoord" row button (same semantics label).
if (pType == 'button' && cType == 'button') {
if (parentKeyed && !childKeyed) {
drop.add(i);
continue;
}
if (!childKeyed) {
final pb = parentUi!.bounds;
final cb = child.bounds;
if (pb != null &&
cb != null &&
pb.width * pb.height > cb.width * cb.height * 1.15) {
drop.add(i);
}
}
continue;
}
// Keep keyed section wrappers even when they contain a visual card. The
// wrapper's testId is an independently usable locator (for example,
// scrollUntilVisible on a measurement section); the child card describes
// its visual content and does not make the wrapper redundant.
}
if (drop.isEmpty) return kept;
return [
for (var i = 0; i < kept.length; i++)
if (!drop.contains(i)) kept[i],
];
});
}