new file mode 100644
@@ -0,0 +1,188 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Copyright (C) 2019, Google Inc.
+ *
+ * media_device_link_test.cpp - Tests link handling on media devices
+ */
+#include <iostream>
+
+#include "device_enumerator.h"
+#include "media_device.h"
+
+#include "test.h"
+
+using namespace libcamera;
+using namespace std;
+
+/*
+ * This test only runs on VIMC: exercising a known media graph makes possible
+ * to make assumptions on the expected results.
+ *
+ * If the VIMC module is not loaded, the test is skipped.
+ */
+class MediaDeviceLinkTest : public Test
+{
+ int init()
+ {
+ enumerator_ = DeviceEnumerator::create();
+ if (enumerator_->enumerate())
+ return TestFail;
+
+ DeviceMatch dm("vimc");
+ dev_ = enumerator_->search(dm);
+ if (!dev_)
+ return TestSkip;
+
+ dev_->acquire();
+
+ if (dev_->open())
+ return TestFail;
+
+ return 0;
+ }
+
+ int run()
+ {
+ /* First of all reset all links in the media graph. */
+ int ret = dev_->disableLinks();
+ if (ret)
+ return TestFail;
+
+ /*
+ * Test if link can be consistently retrieved through the
+ * different methods the media device offers.
+ */
+ MediaLink *link = dev_->link("Debayer A", 1, "Scaler", 0);
+ if (!link) {
+ cerr << "Unable to find link \"Debayer A\"[1] ->"
+ << "\"Scaler\"[0]" << endl
+ << "This link exists in VIMC media graph"
+ << endl;
+ return TestFail;
+ }
+
+ MediaEntity *source = dev_->getEntityByName("Debayer A");
+ if (!source) {
+ cerr << "Unable to find entity \"Debayer A\"" << endl;
+ return TestFail;
+ }
+
+ MediaEntity *sink = dev_->getEntityByName("Scaler");
+ if (!sink) {
+ cerr << "Unable to find entity \"Scaler\"" << endl;
+ return TestFail;
+ }
+
+ MediaLink *link2 = dev_->link(source, 1, sink, 0);
+ if (!link2) {
+ cerr << "Unable to find link \"Debayer A\"[1] ->"
+ << "\"Scaler\"[0]" << endl
+ << "This link exists in VIMC media graph"
+ << endl;
+ return TestFail;
+ }
+
+ if (link != link2) {
+ cerr << "The returned link does not match what expected"
+ << endl;
+ return TestFail;
+ }
+
+ link2 = dev_->link(source->getPadByIndex(1),
+ sink->getPadByIndex(0));
+ if (!link2) {
+ cerr << "Unable to find link \"Debayer A\"[1] ->"
+ << "\"Scaler\"[0]" << endl
+ << "This link exists in VIMC media graph"
+ << endl;
+ return TestFail;
+ }
+
+ if (link != link2) {
+ cerr << "The returned link does not match what expected"
+ << endl;
+ return TestFail;
+ }
+
+ /* After reset the link shall not be enabled. */
+ if (link->flags() & MEDIA_LNK_FL_ENABLED) {
+ cerr << "Link \"Debayer A\"[1] -> \"Scaler\"[0]"
+ << " should not be enabled after a device reset"
+ << endl;
+ return TestFail;
+ }
+
+ /* Enable the link and test if enabling was successful. */
+ ret = link->setEnabled(true);
+ if (ret)
+ return TestFail;
+
+ if (!(link->flags() & MEDIA_LNK_FL_ENABLED)) {
+ cerr << "Link \"Debayer A\"[1] -> \"Scaler\"[0]"
+ << " should now be enabled" << endl;
+ return TestFail;
+ }
+
+ /* Disable the link and test if disabling was successful. */
+ ret = link->setEnabled(false);
+ if (ret)
+ return TestFail;
+
+ if (link->flags() & MEDIA_LNK_FL_ENABLED) {
+ cerr << "Link \"Debayer A\"[1] -> \"Scaler\"[0]"
+ << " should now be disabled" << endl;
+ return TestFail;
+ }
+
+ /* Try to get a non existing link. */
+ link = dev_->link("Sensor A", 1, "Scaler", 0);
+ if (link) {
+ cerr << "Link \"Sensor A\"[1] -> \"Scaler\"[0]"
+ << " does not exist but something was returned"
+ << endl;
+ return TestFail;
+ }
+
+ /* Now get an immutable link and try to disable it. */
+ link = dev_->link("Sensor A", 0, "Raw Capture 0", 0);
+ if (!link) {
+ cerr << "Unable to find link \"Sensor A\"[0] -> "
+ << "\"Raw Capture 0\"[0]" << endl
+ << "This link exists in VIMC media graph"
+ << endl;
+ return TestFail;
+ }
+
+ if (!(link->flags() & MEDIA_LNK_FL_IMMUTABLE)) {
+ cerr << "Link \"Sensor A\"[0] -> \"Raw Capture 0\"[0]"
+ << " should have been 'IMMUTABLE'" << endl;
+ return TestFail;
+ }
+
+ /* Disabling an immutable link shall fail. */
+ ret = link->setEnabled(false);
+ if (!ret) {
+ cerr << "Link \"Sensor A\"[0] -> \"Raw Capture 0\"[0]"
+ << " is 'IMMUTABLE', it shouldn't be disabled"
+ << endl;
+ return TestFail;
+ }
+
+ return 0;
+ }
+
+ void cleanup()
+ {
+ dev_->close();
+ dev_->release();
+
+ delete dev_;
+ delete enumerator_;
+ }
+
+private:
+ DeviceEnumerator *enumerator_;
+ MediaDevice *dev_;
+};
+
+TEST_REGISTER(MediaDeviceLinkTest);
@@ -3,4 +3,10 @@ media_device_print_test = executable('media_device_print_test',
link_with : test_libraries,
include_directories : test_includes_internal)
+media_device_link_test = executable('media_device_link_test',
+ 'media_device_link_test.cpp',
+ link_with : test_libraries,
+ include_directories : test_includes_internal)
+
test('Media Device Print Test', media_device_print_test)
+test('Media Device Link Test', media_device_link_test)
Add a test unit that exercise link handling on the VIMC media graph. Signed-off-by: Jacopo Mondi <jacopo@jmondi.org> --- test/media_device/media_device_link_test.cpp | 188 +++++++++++++++++++ test/media_device/meson.build | 6 + 2 files changed, 194 insertions(+) create mode 100644 test/media_device/media_device_link_test.cpp