[inspect][dart] Introduce (slightly contrived) example of using a child node.
Example output:
$ fx shell iquery --recursive `fx shell iquery --find /hub | grep inspect_mod.cmx | grep root.inspect`
root:
app-color = MaterialColor(primary value: Color(0xff2196f3))
home-page:
title = Hello Inspect!
CF-602 #comment
Change-Id: I9ac9d979ba78eac8cc08ab0e246b4852268158c0
diff --git a/public/dart/fuchsia_inspect/examples/inspect_mod/lib/src/inspect_example_app.dart b/public/dart/fuchsia_inspect/examples/inspect_mod/lib/src/inspect_example_app.dart
index ab39857..dd9c569 100644
--- a/public/dart/fuchsia_inspect/examples/inspect_mod/lib/src/inspect_example_app.dart
+++ b/public/dart/fuchsia_inspect/examples/inspect_mod/lib/src/inspect_example_app.dart
@@ -9,9 +9,9 @@
class InspectExampleApp extends StatelessWidget {
static const _appColor = Colors.blue;
- final inspect.Node _inspect;
+ final inspect.Node _inspectNode;
- InspectExampleApp(this._inspect) {
+ InspectExampleApp(this._inspectNode) {
_initMetrics();
}
@@ -22,20 +22,25 @@
theme: ThemeData(
primarySwatch: _appColor,
),
- home: _InspectHomePage(title: 'Hello Inspect!'),
+ home: _InspectHomePage(
+ title: 'Hello Inspect!',
+ inspectNode: _inspectNode.createChild('home-page')),
);
}
/// Initializes the [Inspect] metrics for this widget.
void _initMetrics() {
- _inspect.createStringProperty('app-color').value = '$_appColor';
+ _inspectNode.createStringProperty('app-color').value = '$_appColor';
}
}
class _InspectHomePage extends StatefulWidget {
final String title;
+ final inspect.Node inspectNode;
- _InspectHomePage({Key key, this.title}) : super(key: key);
+ _InspectHomePage({Key key, this.title, this.inspectNode}) : super(key: key) {
+ inspectNode.createStringProperty('title').value = title;
+ }
@override
_InspectHomePageState createState() => _InspectHomePageState();
diff --git a/public/dart/fuchsia_inspect/lib/src/node.dart b/public/dart/fuchsia_inspect/lib/src/node.dart
index 180d742..b940830 100644
--- a/public/dart/fuchsia_inspect/lib/src/node.dart
+++ b/public/dart/fuchsia_inspect/lib/src/node.dart
@@ -8,14 +8,25 @@
/// A node in the [Inspect] tree that can have associated key-values (KVs).
class Node {
+ // TODO(CF-602): Refactor this to hide implementation details like this index
+ // and the public constructor below (since client code should only create
+ // Nodes using [createChild].
/// The VMO index of this node.
- final int _index;
+ @visibleForTesting
+ final int index;
/// The writer for the VMO that backs this node.
final VmoWriter _writer;
/// Creates a [Node] with the VMO [index] and [writer].
- Node(this._index, this._writer);
+ Node(this.index, this._writer);
+
+ /// Creates a child [Node] with [name].
+ ///
+ /// This method is not idempotent: calling it multiple times with the same
+ /// [name] will create multiple children with the same name.
+ Node createChild(String name) =>
+ Node(_writer.createNode(index, name), _writer);
/// Creates a [StringProperty] with [name] on this node.
///
@@ -23,7 +34,7 @@
/// idempotent and calling it multiple times with the same [name] will
/// create multiple [StringProperty]s.
StringProperty createStringProperty(String name) =>
- StringProperty(name, _index, _writer);
+ StringProperty(name, index, _writer);
}
/// A VMO-backed key-value pair with a string key and string value.
diff --git a/public/dart/fuchsia_inspect/test/node_test.dart b/public/dart/fuchsia_inspect/test/node_test.dart
index bccc7bc..b76aec7 100644
--- a/public/dart/fuchsia_inspect/test/node_test.dart
+++ b/public/dart/fuchsia_inspect/test/node_test.dart
@@ -22,6 +22,20 @@
node = Node(writer.rootNode, writer);
});
+ test('Child nodes have unique indices from their parents', () {
+ var childNode = node.createChild('banana');
+
+ expect(childNode, isNotNull);
+ expect(childNode.index, isNot(node.index));
+ });
+
+ test('Child nodes have unique indices from their siblings', () {
+ var child1 = node.createChild('thing1');
+ var child2 = node.createChild('thing2');
+
+ expect(child1.index, isNot(child2.index));
+ });
+
test('String properties are written to the VMO when the value is set', () {
var property = node.createStringProperty('color')..value = 'fuchsia';