[libcamera-devel] [PATCH 5/5] test: MediaDevice: Add link exercize test

Jacopo Mondi jacopo at jmondi.org
Thu Jan 3 18:38:59 CET 2019


Add test function to exercize the link handling abilities of the media
device.

Signed-off-by: Jacopo Mondi <jacopo at jmondi.org>
---
 test/media_device/media_device_test.cpp | 101 ++++++++++++++++++++++++
 1 file changed, 101 insertions(+)

diff --git a/test/media_device/media_device_test.cpp b/test/media_device/media_device_test.cpp
index c482b2e..99da3a6 100644
--- a/test/media_device/media_device_test.cpp
+++ b/test/media_device/media_device_test.cpp
@@ -42,8 +42,104 @@ private:
 	void printMediaGraph(const MediaDevice &media, ostream &os);
 	void printLinkFlags(const MediaLink *link, ostream &os);
 	void printNode(const MediaPad *pad, ostream &os);
+
+	int exercizeLinks(const MediaDevice &media);
+	int testLink(const MediaDevice &media, MediaLink *link);
 };
 
+int MediaDeviceTest::testLink(const MediaDevice &media, MediaLink *link)
+{
+	MediaPad *sourcePad = link->source();
+	MediaEntity *source = sourcePad->entity();
+	MediaPad *sinkPad = link->sink();
+	MediaEntity *sink = sinkPad->entity();
+
+	cerr << "Test link handling interface on link: "
+	     << source->name() << ":" << sourcePad->index()
+	     << " -> " << sink->name() << ":" << sinkPad->index() << "\n";
+
+	/* Test the link() functions to be consistent. */
+	MediaLink *alink = media.link(source->name(), sourcePad->index(),
+				      sink->name(), sinkPad->index());
+	if (link != alink)
+		return -EINVAL;
+
+	alink = media.link(source, sourcePad->index(),
+			   sink, sinkPad->index());
+	if (link != alink)
+		return -EINVAL;
+
+	alink = media.link(sourcePad, sinkPad);
+	if (link != alink)
+		return -EINVAL;
+
+	/* Fine, we get consisten results... now try to manipulate the link. */
+	int ret = link->enable(true);
+	if (ret)
+		return ret;
+
+	ret = link->enable(false);
+	if (ret) {
+		if (!(link->flags() & MEDIA_LNK_FL_IMMUTABLE))
+			return ret;
+	}
+
+	return 0;
+
+}
+
+/*
+ * Exercize the link handling interface.
+ * Try to get existing and non-existing links, and try to enable
+ * disable link.
+ *
+ * WARNING: this test will change the link between pads on the media
+ * device it runs on, potentially modying the behavior of the system
+ * where the test is run on.
+ */
+int MediaDeviceTest::exercizeLinks(const MediaDevice &media)
+{
+	auto entities = media.entities();
+
+	/* First of all, reset all links in the media graph. */
+	for (MediaEntity *ent : entities) {
+		cerr << "Disable all links in entity: " << ent->name() << "\n";
+
+		for (MediaPad *pad : ent->pads()) {
+			if (pad->flags() & MEDIA_PAD_FL_SINK)
+				continue;
+
+			for (MediaLink *link : pad->links()) {
+				int ret = link->enable(false);
+				if (ret) {
+					if (!(link->flags() &
+					      MEDIA_LNK_FL_IMMUTABLE))
+						return ret;
+				}
+			}
+		}
+	}
+
+
+	/*
+	 * Exercize the link handling interface.
+	 */
+	for (MediaEntity *ent : entities) {
+		for (MediaPad *pad : ent->pads()) {
+			if (pad->flags() & MEDIA_PAD_FL_SINK)
+				continue;
+
+			for (MediaLink *link : pad->links()) {
+				int ret = testLink(media, link);
+				if (ret)
+					return ret;
+			}
+		}
+	}
+
+	return 0;
+}
+
 void MediaDeviceTest::printNode(const MediaPad *pad, ostream &os)
 {
 	const MediaEntity *entity = pad->entity();
@@ -136,6 +232,11 @@ int MediaDeviceTest::testMediaDevice(const string devnode)
 
 	/* Run tests in sequence. */
 	printMediaGraph(dev, cerr);
+
+	ret = exercizeLinks(dev);
+	if (ret)
+		return ret;
+
 	/* TODO: add more tests here. */
 
 	dev.close();
-- 
2.20.1



More information about the libcamera-devel mailing list