buildObservedElementTree function
({List<UiElement> elements, Map<String, SnapshotElementHandle> handles})
buildObservedElementTree({
- required WidgetTester tester,
- required AssertionEngine assertions,
- required bool includeBounds,
- bool keyedOnly = false,
- bool enableSemantics = true,
- bool useSemantics = true,
- bool registerRouteDependency = true,
Builds the kept observe element tree from the live Flutter element walk.
When enableSemantics is true (live observation), briefly enables a
SemanticsHandle for richer labels. Diagnostic / report snapshots must
pass enableSemantics: false so hot-path captures never toggle semantics.
Implementation
({List<UiElement> elements, Map<String, SnapshotElementHandle> handles})
buildObservedElementTree({
required WidgetTester tester,
required AssertionEngine assertions,
NavigationTestService? navigation,
required bool includeBounds,
bool keyedOnly = false,
bool enableSemantics = true,
bool useSemantics = true,
bool registerRouteDependency = true,
}) {
return RunnerBenchmark.sync('observer', 'buildObservedElementTree', () {
final kept = <({Element element, UiElement ui})>[];
final handles = <String, SnapshotElementHandle>{};
var index = 0;
final seenRenderObjects = <Object>{};
final claimedOwnedIds = <String>{};
final ancestry = _ObservationAncestry();
SemanticsHandle? semantics;
if (enableSemantics) {
semantics = tester.ensureSemantics();
}
try {
final viewportSize =
tester.view.physicalSize / tester.view.devicePixelRatio;
final routeName = navigation?.currentRoute?.trim();
for (final element in tester.allElements) {
RunnerBenchmark.count('elementsVisited', 1);
// Flutter paints text selection handles in OverlayEntries. Their small
// gesture/painter subtree can look exactly like an app icon/button to
// the generic observer classifier, but it is an editing affordance for
// the focused text field, not independent screen content.
if (ancestry.inclusive(element).selectionHandle) continue;
// allElements includes widgets retained by inactive routes (including
// the previous screen beneath a pushed route). They are neither current
// screen content nor scrollable offscreen targets, so exclude them
// before classifying or serializing observer nodes.
final ancestors = ancestry.ancestorsOf(element);
if (!ancestors.routeCurrent) continue;
// Some navigators retain route content in an Offstage subtree while a
// transition or route replacement is in progress. Geometry alone would
// label those nodes `offscreen=true`, incorrectly suggesting scrolling
// can reveal them.
if (ancestors.offstage) continue;
final ownedKey = ancestry.inclusive(element).selfKeyed;
final ownedId = readOwnedWidgetLocatorId(element);
if (keyedOnly && ownedKey == false && ownedId == null) continue;
final underKeyed = ancestors.keyed;
var keep = false;
// Owned keys / primaries may inherit exclusive wrapper ids; nested
// content under a card must not be stamped with the parent card id.
var allowInheritedId = false;
if (ownedKey) {
if (_isPageShellElement(
element,
ownedId,
viewportSize,
routeName: routeName,
)) {
keep = false;
} else {
keep = true;
allowInheritedId = true;
if (ownedId != null) claimedOwnedIds.add(ownedId);
}
} else if (isPrimaryControlElement(element) &&
!hasPrimaryControlAncestor(element)) {
if (readInvokableLocatorId(element) != null &&
nearestDescendantValueKeyLocatorId(element) != null) {
keep = false;
} else {
final scopeId = ownedId ??
nearestExclusiveKeyedWrapperId(
element,
viewport: viewportSize,
routeName: routeName,
);
if (scopeId != null) {
keep = claimedOwnedIds.add(scopeId);
} else {
keep = true;
}
allowInheritedId = keep;
}
} else {
keep = _keepUnkeyedElement(element, underKeyedAncestor: underKeyed);
}
if (!keep) continue;
final testId = ownedId ??
(allowInheritedId
? (observeLocatorId(
element,
viewport: viewportSize,
routeName: routeName,
) ??
'')
: '');
RunnerBenchmark.count('elementsKept', 1);
final elementId = 'el_$index';
final uiElement = describeElement(
element: element,
elementId: elementId,
testId: testId.isEmpty ? null : testId,
assertions: assertions,
tester: tester,
includeBounds: includeBounds,
useSemantics: useSemantics,
registerRouteDependency: registerRouteDependency,
);
// Generic gesture wrappers with no key, label, text, or generated
// action have no usable identity for the runner or a downstream
// consumer. The
// integration binding can expose transparent host-area gestures as
// button-shaped nodes; publishing them only adds misleading controls to
// the observed screen.
if (_isUnaddressableAnonymousButton(uiElement)) continue;
final renderObject = element.renderObject;
if (testId.isEmpty &&
renderObject != null &&
!seenRenderObjects.add(renderObject)) {
continue;
}
index++;
kept.add((element: element, ui: uiElement));
handles[elementId] = SnapshotElementHandle(
observationId: '',
elementId: elementId,
testId: testId.isEmpty ? null : testId,
element: element,
observableFingerprint: fingerprintForElement(uiElement),
);
}
} finally {
semantics?.dispose();
}
final absorbed = absorbFormFieldLabels(kept);
final deduped = dropRedundantNestedObserveLeaves(absorbed);
final remainingIds = <String>{
for (final item in deduped) item.ui.elementId,
};
handles.removeWhere((id, _) => !remainingIds.contains(id));
for (final item in deduped) {
handles[item.ui.elementId] = SnapshotElementHandle(
observationId: '',
elementId: item.ui.elementId,
testId: item.ui.testId,
element: item.element,
observableFingerprint: fingerprintForElement(item.ui),
);
}
return (elements: nestKeptElements(deduped), handles: handles);
});
}